Java 2 Flashcards
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)
- inherits properties/variables and behavior/methods of another class
- Great for code Reusability and Maintainability
Example of this: When i have to set up API i can extend the properties such as the BaseURI of the SpartanBase Class - have to use extends keyword
- IS-A relation between classes
If Vehicle is parent class then
Car IS-A Vehicle
Bus IS-A Vehicle - Can only extend one class
- Constructors are NOT Inherited
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?
- Can have its own instance variables and methods
- Can use parents variables and methods using:
- super.instanceVariable
- super.instanceMethod
Method hiding:
- There is a difference between hiding a static method and overriding.
The version of the overridden instance method that gets invoked is the one in the subclassthe version of the hidden static method that gets invoked depends on where it is invoked from the superclass or subclass.
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
- Child class can extend itself to another class
- One class can serve as parent for many child classes
- Java only allows you to extend one class
How does inheritance work given the same/different packages when it comes to access modifiers?
In same package:
- public
- protected
- default
- private is never inherited no matter what
Different Package:
- public
- protected
- default
Inheritance: Super Keyword
Super keyword is used to call the constructor
- it can call any of the constructor of the parent as long as the same parameters are given.
- has to be the first line of the code
- benefits: reusability logic, no new code is needed
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:
- Abstract class
- Interface
- Abstract classes can be extended to child classes
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 {
}