OOP FUNDAMENTALS Flashcards
What does a subclass inherit from a superclass
attributes, methods and associations
What are the capabilities of a subclass in terms of functionalities?
The subclass can:
- Add new functionality
- Use inherited functionality
- Override inherited functionality
What are the different types of inheritance and what kind does java support?
Java supports single inheritance , meaning that a derived class can have only one parent class • Multiple inheritance allows a class to be derived from two or more classes, inheriting the members of all parents • Java does not support multiple inheritance • In most cases, the use of interfaces gives us aspects of multiple inheritance without the overhead
Describe the inheritance hierarchy
Inheritance creates a class hierarchy
- Classes higher in the hierarchy are more general and more abstract
- Classes lower in the hierarchy are more specific and concrete
- At the very top of the inheritance tree is a class called Object
- All Java classes inherit from Object.
- All objects have a common ancestor
- The Object class is defined in the java.lang package
Discuss visibility modifiers in relation to inheritance
- Visibility modifiers affect the way that class members can be used in a child class
- For instance, variables and methods declared with private visibility cannot be referenced by name in a child class
- They can be referenced in the child class if they are declared with public visibility – but public variables violate the principle of encapsulation
- There is a third visibility modifier that helps in inheritance situations: protected
- The protected modifier allows a child class to reference a variable or method directly in the child class
- It provides more encapsulation than public visibility, but is not as tightly encapsulated as private visibility
- A protected variable is visible to any class in the same package as the parent class
Discuss constructors in relation to inheritance and give examples
When a subclass object is created, its constructor is called. It is the responsibility of the subclass constructor to invoke the appropriate superclass constructors so that the instance variables defined in the superclass are properly initialized ● Superclass constructors can be called using the "super" keyword in a manner similar to "this" It must be the first line of code in the constructor ● If a call to super is not made, the system will automatically attempt to invoke the no-argument constructor of the superclass.
Eg public class OverdraftAccount extends BankAccount { private float overdraftLimit; public OverdraftAccount(int anAccountNumber, String aName, float aLimit) { super(anAccountNumber, aName); overdraftLimit = aLimit; }
Discuss method overiding in relation to inheritance and give examples
Sometimes, the implementation of the method in the superclass does not provide the functionality required by the subclass. In these cases, the method must be overridden. ● To override a method, provide an implementation in the subclass. The method in the subclass MUST have the exact same signature as the method it is overriding. ● If a method is declared with the final modifier, it cannot be overridden
Eg public class BankAccount { protected float balance; public void deposit(float anAmount) { if (anAmount>0.0) balance = balance + anAmount; } }
public class OverdraftAccount extends BankAccount { private float limit; public void withdraw(float anAmount) { if ((anAmount>0.0) && (getBalance()+limit>anAmount)) balance = balance - anAmount; } }
What are the two main kinds of polymorphism
- Overloading
• Two or more methods with same name but different signatures
2.Overriding
• Replacing an inherited method with another having the same signature
What are the three types of abstraction
1.Data Abstraction
Programming languages define constructs to simplify the way information is presented to the programmer.
2.Functional Abstraction
Programming languages have constructs that ‘gift wrap’ very complex and low level instructions into instructions that are much more readable.
3.Object Abstraction
OOP languages take the concept even further and abstract programming constructs as objects
What is accessibility scope?
Accessibility scope defines the boundary of access to a class and its members
Give different types of scopes and the access they provide
static-static code can access static members but not instance members
non-static-non-static code can access both static members and instance members
package-a class and its members can be accessed within the package they are declared
class-class members can be accessed within the class
block- local variables can be accessed only within a block
Explain any 3 differences between an interface and abstract class
1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. Since Java 8, it can have default and static methods also.
2) Abstract class doesn’t support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
4) Abstract class can provide the implementation of interface. Interface can’t provide the implementation of abstract class.
5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
6) An abstract class can extend another Java class and implement multiple Java interfaces. An interface can extend another Java interface only.
7) An abstract class can be extended using keyword “extends”. An interface can be implemented using keyword “implements”.
8) A Java abstract class can have class members like private, protected, etc. Members of a Java interface are public by default.
9)Example Abstract class: public abstract class Shape{ public abstract void draw(); } Example Interface: public interface Drawable{ void draw(); }
A java beginner has had of the statement “java is compiled then interpreted” Explain to
them why this claim is true
In Java, programs are not compiled into executable files; they are compiled into bytecode , which the JVM (Java Virtual Machine) then interprets / executes at runtime.
Explain why you may need a java API and give one example of an API you may use stating
its purpose
APIs are important software components bundled with the JDK. APIs in Java include classes, interfaces, and user Interfaces. They enable developers to integrate various applications and websites and offer real-time information.
eg RESTful API, WEB API