Unit 5
Unit 5
Note: Java does not support multiple inheritance with classes. In Java, we can achieve multiple
inheritance only through the Interfaces.
}
}
Multilevel Inheritance: Example
class GrandParent {
int a = 10;
}
class Parent extends GrandParent {
int b = 20;
}
class Child extends Parent {
int c = 30;
public static void main(String args[]) {
Output:
Child obj = new Child(); GrandParent class parameter = 10
System.out.println(“GrandParent class parameter = ”+obj.a); Parent class parameter = 20
System.out.println(“Parent class parameter = ”+obj.b); Child class parameter = 30
System.out.println(“Child class parameter = ”+obj.c);
}
}
Hierarchical Inheritance: Example
class Parent { int a = 10; }
class Child1 extends Parent { int b = 20; }
class Child2 extends Parent { int c = 30; }
class Test {
public static void main(String args[]) {
Child1 obj1 = new Child1();
Child2 obj2 = new Child2(); Output:
Parent class parameter = 10
System.out.println(“Parent class parameter = ”+obj1.a);
Child1 class parameter = 20
System.out.println(“Child1 class parameter = ”+obj1.b); Child2 class parameter = 30
System.out.println(“Child2 class parameter = ”+obj2.c);
}
}
Method Overriding
• Method Overriding means child class has the same method as declared in the parent class.
• Method overriding is used to provide the specific implementation of a method which is already
provided by its superclass.
• Method overriding is used for runtime polymorphism.
• Note: By implementing method overriding, we can access only child class methods as parent
class methods will be overridden. To access parent class methods, we have to use ‘super’
keyword.
Method Overriding: Example
class A {
void show() { System.out.println(“Parent class method called.”); }
}
class B extends A {
void show() { System.out.println(“Child class method called.”); }
}
class Test {
public static void main(String[] args) {
Output:
B obj = new B(); Child class method called.
obj.show();
}
}
‘super’ keyword
• The super keyword in Java is a reference variable which is used to refer parent class object.
• It is used to call superclass variable, superclass method and superclass constructor.
• The most common use of the super keyword is to eliminate the confusion between superclasses
and subclasses that have methods with the same name.
• Syntax:
1. To access super class variable: super.variable_name <=value>;
2. To access super class method: super.method_name();
3. To access super class constructor: super();
‘super’ keyword: Example
1. To access super class variable:
class Parent {
int a = 10;
}
class Child extends Parent {
int a = 20;
public static void main(String[] args) {
Output:
System.out.println(“Parent class parameter = ” + super.a); Parent class parameter = 10
System.out.println(“Child class parameter = ” +a); Child class parameter = 20
}
}
‘super’ keyword: Example
2. To access super class method:
class A {
void show() { System.out.println(“Parent class method called.”); }
}
class B extends A {
void show() {
super.show();
System.out.println(“Child class method called.”);
Output:
} Parent class method called.
public static void main(String[] args) { Child class method called.
B obj = new B();
obj.show();
}
}
‘super’ keyword: Example
3. To access super class constructor:
class A {
A() { System.out.println(“Parent class constructor called.”); }
}
class B extends A {
B() {
super();
System.out.println(“Child class constructor called.”);
} Output:
public static void main(String[] args) { Parent class constructor called.
Child class constructor called.
B obj = new B();
}
}
‘final’ keyword
• The final keyword in java is used to restrict the user.
• Final keyword used in Java to restrict the modification of a variable, method, or class.
• Output: Compile Time Error (because value of a final variable can not be modified.)
‘final’ keyword: Example
2. Final Method:
class A {
final void show() {
}
}
class B extends A {
void show() { //This line will generate an error.
}
public static void main(String[] args) {
System.out.println(“Final Method Demo.”);
}
}
• Output: Compile Time Error (because final method can not be override.)
‘final’ keyword: Example
3. Final Class:
final class A {
}
class B extends A {//This line will generate an error.
public static void main(String[] args) {
System.out.println(“Final Class Demo.”);
}
}
• Output: Compile Time Error (because final class can not be inherited.)
Interface
• An interface in java is a blueprint of a class. It is also known as set of rules for classes.
• It has final, static variables and abstract methods only.
• Method body will be provided into classes which will implement that interface. But that method
should be declared as public in a class.
• The interface in java is a mechanism to achieve abstraction.
• It cannot be instantiated just like abstract class.
• Java Interface also represents IS-A relationship (inheritance in iterface).
• Java supports multiple inheritance in terms of interfaces only.
• Classes have to use ‘implements’ keyword to use Interface.
• Syntax:
interface <interface_name>{
// declare static constant fields and abstract methods
}
Interface: Example
interface printable {
abstract void print();
}
class Main implements printable {
public void print() {
System.out.println(“Interface Demo");
}
public static void main(String args[]) { Output:
Main obj = new Main(); Interface Demo
obj.print();
}
}
Abstract Class v/s Interface
Sr. No. Abstract Class Interface
1. Abstract class can have abstract and non- Interface can have only abstract methods. Since
abstract methods. Java 8, it can have default and static
methods also.
2. Abstract class doesn't support multiple Interface supports multiple inheritance.
inheritance.
3. Abstract class can have final, non-final, static Interface has only static and final variables.
and non-static variables.
4. Abstract class can provide the implementation of Interface can't provide the implementation of
interface. abstract class.
5. The abstract keyword is used to declare abstract The interface keyword is used to declare
class. interface.
6. An abstract class can be extended using keyword An interface can be implemented using keyword
"extends". "implements".
Abstract Class v/s Interface
Sr. No. Abstract Class Interface
7. A Java abstract class can have class members like Members of a Java interface are public by
private, protected, etc. default.
8. An abstract class can extend another Java class and An interface can extend another Java interface
implement multiple Java interfaces. only.
9. Example: Example:
• Syntax:
interface_name reference_variable = new ClassName();
Interface Reference: Example
interface A {
void show();
}
class Test implements A {
public void show() {
System.out.println(“Interface Reference Demo”);
}
public static void main(String args[]) { Output:
A ref = new Test(); Interface Reference Demo
ref.show();
}
}
‘instanceof’ operator
• It is used to test whether the object is an instance of the specified type (class or subclass or
interface).
• The instanceof in java is also known as type comparison operator because it compares the
instance with type.
• It returns either true or false. If we apply the instanceof operator with any variable that has null
value, it returns false.
• Example:
class Test {
public static void main(String args[]) { Output:
Test obj = new Test(); true
System.out.println(obj instanceof Test);
}
}
Interface Inheritance
• As shown in the figure given below, a class extends another class, an interface extends another
interface, but a class implements an interface.
Interface Inheritance: Example
interface Printable { void print(); }
interface Showable extends Printable{ void show(); }
class A implements Showable {
public void print(){ System.out.println(“Print method called."); }
public void show(){ System.out.println(“Show method called."); }
public static void main(String args[]){
A obj = new A();
Output:
obj.print(); Print method called.
obj.show(); Show method called.
}
}
Multiple Inheritance in Interface: Example
interface Printable { void print(); }
interface Showable { void show(); }
interface Display extends Printable, Showable{ void disp(); }
class A implements Display {
public void print(){ System.out.println(“Print method called."); }
public void show(){ System.out.println(“Show method called."); }
public void disp(){ System.out.println(“Disp method called."); }
public static void main(String args[]){
A obj = new A(); Output:
obj.print(); Print method called.
Show method called.
obj.show(); Disp method called.
obj.disp();
}
}
Dynamic Method Dispatch
• It is also known as Runtime Polymorphism.
• It is a process in which a call to an overridden method is resolved at runtime rather than compile-
time.
• In this process, an overridden method is called through the reference variable of a superclass.
• The determination of the method to be called is based on the object being referred to by the
reference variable.
• Upcasting: If the reference variable of Parent class refers to the object of Child class, it is
known as upcasting. For example:
Dynamic Method Dispatch: Example
class Bike {
void run() { System.out.println(“Bike Class Method "); }
}
class Honda extends Bike {
void run() { System.out.println(“Honda Class Method"); }
public static void main(String args[]) {
Output:
Bike b = new Honda(); //upcasting Honda Class Method
b.run();
}
}
Java Object Class
• The Object class is the parent class of all the classes in java by default.
• In other words, it is the topmost class of java.
• The Object class is beneficial if you want to refer any object whose type you don't know.
• Notice that parent class reference variable can refer the child class object, known as upcasting.
• The Object class provides some common behaviors to all the objects such as:
1. object can be compared
2. object can be cloned
3. object can be notified
etc.
Java Object Class Methods
Sr. No. Method Description
1. public final Class getClass() returns the Class class object of this object. The Class class can
further be used to get the metadata of this class.
2. public boolean equals(Object obj) compares the given object to this object.
3. public String toString() returns the string representation of this object.
4. public final void notify() wakes up single thread, waiting on this object's monitor.
5. public final void notifyAll() wakes up all the threads, waiting on this object's monitor.
6. public final void wait() throws causes the current thread to wait, until another thread notifies
InterruptedException (invokes notify() or notifyAll() method).
7. protected void finalize() throws is invoked by the garbage collector before object is being
Throwable garbage collected.
Understanding System.out.println
• In Java, System.out.println() is a statement which prints the argument passed to it.
• The println() method displays results on the monitor in new line.
• System is the class name, it is declared as final.
• The out is an instance of the System class and is of type PrintStream.
• Its access specifiers are public and final.
• It is an instance of java.io.PrintStream.
• When we call the member, a PrintStream class object creates internally.
• So, we can call the print() method, as shown below:
System.out.print();
• It creates the PrintStream class object. This object, by default, represents the output device, i.e., the
monitor.
Understanding System.out.println
• Example:
class Demo {
public static void main(String args[]) {
Output:
System.out.print("Hello!"); Hello! Java
System.out.print("Java");
}
}