Lesson 5
Lesson 5
Default and Protected have package level access – both are same if your class has no Inheritance
If you have Inheritance relationship, even Protected can access other package too
Child class / Derived class (More specific, It can access everything from the parent except
private fields and methods
If you have no constructors in both Parent and Child – Default constructor is automatic – No
Compilation Error
My Parent class have Default Constructor and my child class has no constructor – fine,
No compilation error – Automatic
If you have no default constructor in your parent, you have emplicit constructor in your
parent, must make use of any one parent constructor in your child class.
main(){
employee arraylist collections, Linked list manager collections
print(employee)
print(manager);
// API
}
public void print(List[] ea){
// Do something
}
The Number class in Java is an abstract superclass for numeric wrapper classes
like Integer, Double, Float, Long, Short, and Byte. It provides methods to convert its instances
into different primitive types. However, it does not directly support arithmetic operations
because it serves as a generalized abstraction.
Number x = 20;
Number y = 20;
//Number z = x + y; // Cause compilation error
Right Usage
Number x = 20;
Number y = 20;
Double z1 = x.doubleValue() + y.doubleValue();
Number z2 = x.doubleValue() + y.doubleValue();
int sum = x.intValue() + y.intValue();
The Number class primarily provides methods for type conversion and polymorphic behavior.
While you cannot directly perform arithmetic or other operations on Number, you can use type-
specific methods (like intValue(), doubleValue()) to work with the underlying value.
Key differences between method overloading and method overriding in Java:
Lesson 5 – Day – 2
Circle, Square, Triangle – All three has no relationship and no common type(Then we can use
Object type)
Interface
Pre-Java-8 Interface
è Pre-Java-8 – All methods are unimplemented, by default abstract methods
è By default all the fields are public static final
è By default all the methods public abstract à Unimplemented
è You cannot create object for an interface
è You cannot create constructor inside an interface
è You sub class can implements Interface
è One class can implement multiple interface
è One interface can inherit from other interface using extends keyword. May inherit one or
multiple interface
If you add some methods in the interface it affects your implementation. TO overcome this
drawback Java 8 introduces two implemented methods
1. Default
2. Static
You can able to override the Default implementation inside the class or you can go with
interface default implementation.
JDK 9
Reflection Library
Retrieving a Class type is a fundamental part of Java Reflection. It allows you to examine or
modify the runtime behavior of applications. For example, you can dynamically create
objects, access fields, invoke methods, and much more.
Class.forName() is useful for dynamic class loading, where the class name is not known until
runtime. This is particularly useful in frameworks and libraries that need to load user-defined
classes or configurations.
Functional Interface – An interface has only one unimplemented method. This can be
implemented Lambdas. Example: Comparable
Marker Interface – Interface has no methods is called Mark interface. ( No methods which
useful to do cloning, data persistency). Example - API: Serializable
@FunctionalInterface
Interface test{
void display();
}
Object class
Case 1: You should have only one equals() from the parent, the comparison depends on
parent attributes, you can use instanceof statategy. PersonwithJob is a Person.
Case 2: Same class Strategy, you should have two equals - one parent and the child. You can
compare Person with Person type and PersonwithJob with PersonwithJob.
1. Shallow clone
2. Deep clone.
Inside your class there is no composition – No worries you will get a copy.
When have a composition need to do deep copy.