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 {
}
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
- Default methods can only exist in an interface
- default keyword is mandatory to declare a default method
- default methods cannot be static, final or abstract
- methods are automatically public and cannot be changed
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 } }
What are the only fields accepted in interface class?
Yes, an Interface class can only have constant variables public static final int CONSTANT = 10;
How do you inherit and interface class?
Must use the implement keyword. Can implement multiple interfaces
In an interface class are methods are…
public abstract