Method Overriding, Abstract Class and Interface in Java

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

Method overriding, Abstract Class and

Interface in Java

Ms. Jagruti A. Wagh

Assistant Professor. MMCOE, Pune


METHOD OVERRIDING

If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding in Java.
2
METHOD OVERRIDING

Rules for Java Method Overriding


1. The method must have the same name as in the
parent class
2. The method must have the same parameter as in
the parent class.
3. There must be an IS-A relationship (inheritance).

In Java, we can override methods only, not the variables(data members),


so runtime polymorphism cannot be achieved by data members.
3
class Vehicle{
void run()
{
System.out.println("Vehicle is running");}
}
class Car extends Vehicle
{
void run()
{
System.out.println(“Car is running safely");
}

public static void main(String args[])


{
Car obj = new Car();
obj.run();
}
}
4
Dynamic Method Dispatch

● Dynamic method dispatch is the mechanism in


which a call to an overridden method is resolved
at run time instead of compile time.
● This is an important concept because of how Java
implements run-time polymorphism.

5
class A
{
void m1()
{
System.out.println("Inside A's m1 method");
}}

class B extends A
{
void m1() // overriding m1()
{
System.out.println("Inside B's m1 method");
}}

class C extends A
{
void m1() // overriding m1()
{
System.out.println("Inside C's m1 method");
}}

class Dispatch
{
public static void main(String args[])
{ A a = new A();
B b = new B();
C c = new C(); 6
// obtain a reference of type A
A ref;

// ref refers to an A object


ref = a;

// calling A's version of m1()


ref.m1();

// now ref refers to a B object


ref = b;

// calling B's version of m1()


ref.m1();

// now ref refers to a C object


ref = c;

// calling C's version of m1()


ref.m1();
}
}
7
ABSTRACT CLASS

8
ABSTRACT CLASS
abstract class Bike{
abstract void run();
}
class Honda extends Bike{
void run()
{System.out.println("running safely");}
public static void main(String args[]){
Bike obj = new Honda ();
obj.run();
} } 9
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 when want to refer
any object whose type you don't know.
● Parent class reference variable can refer the child
class object, know as upcasting.
● Methods of object class are inherited by every
object created in a Java program.

10
11
12
13
14
15
The equals( ) method compares two object reference, to see whether they refer to the same object

16
toString( ) method

• The toString( ) method returns a string that


contains a description of the object on which
it is called.
• If you want to represent any object as a
string, toString() method comes into
existence.
• Many classes override this method.
• This method is automatically called when an
object is output using println( ).
17
Program : Without Overriding toString

18
Program : With Overriding toString

19
INTERFACE

20
INTERFACE

24-01-2021 21
INTERFACE (cont…)

22
interface Bank {
float rateOfInterest();
}
class SBI implements Bank{
public float rateOfInterest() { return 9.15f; }
}
class ICICI implements Bank{
public float rateOfInterest() { return 9.7f; }
}
class TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest()); }} 23
interface Printable{
public static void main(String args[])
void print(); }
{
interface Showable extends Printable
TestInterface4 obj = new TestInterface4();
{

void show();
obj.print();
}
obj.show();
class TestInterface4 implements Showable
}
{
}
public void print()

{System.out.println("Hello");}

public void show()

System.out.println("Welcome");

}
24
25
26
NESTED INTERFACE

An interface can have another interface which is


known as a nested interface


Nested interface must be public if it is declared
inside the interface but it can have any access
modifier if declared within the class.

Nested interfaces are declared static implicitly.

27
NESTED INTERFACE
Syntax of nested interface which is declared within the interface

interface interface_name{
...
interface nested_interface_name{
...
}
}

28
NESTED INTERFACE

Syntax of nested interface which is declared within the class

class class_name{

...

interface nested_interface_name{

...

29
30

You might also like