Nested Classes:
public class FlightTicket {
public static class Business {
}
public static class Economy {
}
public static class First {
} }how do i create an object of any of the nested classes?
FlightTicket.Business business = new FlightTicket.Business();
etc.
Inheritance principles: (7 in total)
Inheritance: Method Overloading
-2 methods with the same name and parameters
- Child class can have a different implementation of the same method signature as the parent.
- Method in child class must be at least as accessible or more accessible as the method in parent class.
- Therefore, cannot override a public method in parent class with a method that is private. etc.
- Method return must be the same of a covariant.
Example: A method that returns a person can be overridden with a method that returns student since Student IS-A Person
Inheritance: Child Class Can a child class use Parents Variables or methods?
Method hiding:
if the child class defines the method as static with same method signature as parent class, then the method in the child class hides the one in parent class.
Example:
Parent Class:
public static void testClassMethod(){
Sysout(" A - Parent")
}
public void testInstanceMethod(){
Sysout(" B - Child")
}
Child Class:
public static void testClassMethod(){
Sysout(" A - Child ")
}
public void testInstanceMethod(){
Sysout(" B - Child ")
}Parent parent = new Parent(); Child myChild = parent;
Child.testClassMethod(); // Sysout(“ A - Parent”)
myChild.testInstanceMethod; //Sysout(“ B - Child “)
Multi-Level Inheritance
Hierarchical Inheritance
Multiple Inheritance
How does inheritance work given the same/different packages when it comes to access modifiers?
In same package:
Different Package:
Inheritance: Super Keyword
Super keyword is used to call the constructor
Final class
cannot be extended, cannot be inherited
Final method
cannot be overriden in sub class
Final Variable
Cannot be reassigned to a new value
Access modifiers, what are they and explain each one.
Private > default > protected > public
private: is only visible/accessible in same class. Private method and variables are not inherited
default- only visible/accessible in same package only. Not visible or accessible to any other class in different package. default variables/methods are only when sub class and parent class are in the same package
protected - visible/accessible in same package or sub classes in different packages
public visible/accessible to very class, in same package or another. all variables and methods are inherited
Constructor and Inheritance
When we have a parent-child relation between two classes and we make an object of the child class. The parent’s constructor will run first and create an object of the Parent then of the child
Abstraction - OOP
2 Ways to use abstraction:
What keyword has to be at the top of the class for it to be considered abstract?
abstract keyword must be added to class Name
ex: abstract class Animal {}
Can abstract classes be instantiated to a object ?
Cannot create objects of abstract classes
In the abstract what can/cannot do for method
Only declare method but cannot implement any code
implement will be done in child class
- Sub classes are called concrete class
can an abstract class contain abstract methods and non-abstract methods
Yes they can
Major Benefit of Abstract class
To store and reuse variables and methods and do a unique implementation in each child class
Abstraction - Interface type
Must have the interface keyword on the top
- public interface Teach{
}
Can you extend multiple interfaces ?
Can you create an object of Interface class?
Yes you can, its one of the benefits of interface
No you cannot create object
For an interface class, can you have a constructor?
No you cannot have a constructor for interface class
Rules of an Interface class
Static keyword in interface
- static method was added since java 8
public interface Teachable {
public abstract void canLean();
public static void practice(){
//implementation code here
}
}