0% found this document useful (0 votes)
11 views

Java Inheritance

Uploaded by

Yash Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Java Inheritance

Uploaded by

Yash Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Java Lecture-29
Topic: Inheritance
Inheritance :
 Creating a new class from existing class is referred as Inheritance. OR

 When one class acquires the properties of another class then it is referred
as inheritance.

 New class which is created from existing class is referred as subclass.

 Existing class from which new class is created is referred as super class.

 In OOP, concept of inheritance provides the idea of Reusability of code.

 "extends" keyword is used to perform the inheritance in Java.

 Syntax of Inheritance :

class <subclass> extends <superclass>


{
// Body of sub class
}

Base Class
Super class

Derived Class
Sub Class

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Types of Inheritance
There are three types of Inheritance in Java
(1) Single Inheritance
(2) Multi-level Inheritance
(3) Hierarchical Inheritance

Note : Java does not support multiple and hybrid inheritance. But this
concept is implemented by using an interface.

1) Single Inheritance :
If there is only one subclass and only one super class then it is referred
as single inheritance

Super class

Sub Class

Following Program demonstrate Single Inheritance


class A
{
int x;

void setX(int a)
{
x=a;
}
}

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

class B extends A
{
int y;

void setY(int a)
{
y=a;
}
void display()
{
System.out.println("x="+x);
System.out.println("y="+y);
}
public static void main(String [] args)
{
B b = new B();
b.setX(10);
b.setY(20);
b.display();
}
}
Output: x=10
y=20

2) Multi-level Inheritance
When a class extends a class, which extends another class then this is
called multilevel inheritance. For example class C extends class B and
class B extends class A then this type of inheritance is known as
multilevel inheritance.
A

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Following Program demonstrate Multilevel Inheritance


class A
{
int x;
void setX(int a)
{
x=a;
}
}
class B extends A
{
int y;
void setY(int a)
{
y=a;
}
}
class C extends B
{
int z;
void setZ(int a)
{
z=a;
}
void display()
{
System.out.println("x="+x);
System.out.println("y="+y);
System.out.println("z="+z);
}
public static void main(String [] args)
{
C c = new C();
c.setX(10);
c.setY(20);
c.setZ(30);
c.display();
}
}

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

3) Hierarchical Inheritance :
It two or more classes are derived from same base class, then it is referred as
hierarchical inheritance.

B C
class B extends A class C extends A

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Java Lecture-30
Topic: ‘super’ keyword

Use of Super :
Whenever a subclass needs to refer to its immediate super class, it can do so
by use of the keyword "super".
There are two uses of super
1) Using super() method to call a super class constructor.
2) Using super keyword to access the hidden member of super class in
subclass.

1) Using super to call super class constructor :


The subclass constructor uses the keyword super to invoke the constructor
method of the super class.
A subclass can call a constructor method define by its super class by use of
the following form of super.
super(parameter_list);
Here, parameter_list specifies any parameter needed by the constructor of
the super class.

The keyword super is used under following conditions


 The call to super class constructor must appear as the first statement
within the subclass constructor.
 The parameters in the super call must match the order and type of the
variable declared in the super class constructor.

Following program demonstrate use of super method


class Person
{
String name;

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

int age;

Person(String s, int a)
{
name=s;
age=a;
}
}
class Employee extends Person
{
double salary;
Employee(String s, int a, double sal)
{
super(s,a); // calls super class constructor
salary=sal;
}

void display()
{
System.out.println("Emp_Name : "+name);
System.out.println("Emp_Age : "+age);
System.out.println("Emp_Salary: "+salary);
}
}
class SuperTest
{
public static void main(String [] args)
{
Employee e = new Employee("Atul",45,50000.0);
e.display();
}
}
Output:

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Java Lecture-31
Topic: Method Overloading and Method Overriding

Method Overloading :
 Overloading occurs if several methods have the same name but different
parameters.
 The compiler call the correct method by matching the parameter types of
the various methods with the types of values used in the specific method
call.
 A Compile Time error occurs if the compiler cannot match the parameter.
Example:
class sample
{
int max (int a, int b)
{
if (a > b)
return (a);
else
return (b);
}
double max (double a, double b)
{
if (a>b)
return (a);
else
return (b);
}
public static void main(String [] args)
{
Sample s = new Sample ( );
System.out.println (“Maximum between 10 & 20 is” + s.max (10, 20));
System.out.println (“Maximum between 3.4 and 1.7 is” + s.Max (3.4, 1.7));
}
}
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Output: Maximum between 10 and 20 is 20


Maximum between 3.4 and 1.7 is 3.4

Method Overriding :
 In Inheritance, when a method in a subclass has the same name and
same parameters list as a method in it super class, then the method in
the subclass is said to override the method in the super class.

 When an overridden method is called from subclass, it will always refer


to the version of that method defined by the subclass.

Example :
class A
{
void display ( )
{
System.out.println(“From A class display”) ;
}
}
class B extends A
{
void display ( )
{
System.out.println (“From B class display”);
}
public static void main(String [] args)
{
B b = new B ( );
b.display ( ) ;
}
}
Output: From B class display.
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Difference between method overloading & Method overriding

Method Overloading Method Overriding


1) Method overloading is 1) Method overriding is
performed within class. performed within subclass.
2) In case of method 2) In case of method overriding,
overloading, parameter list parameter list must be same.
must be different
3) Method overloading is 3) Method overriding is the
example of compile-time example of run-time
polymorphism. polymorphism.
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Java Lecture-32
Topic: Dynamic Method Dispatch

Dynamic Method Dispatch (DMD) :


 DMD is the mechanism by which a call to an overridden method is
resolved at runtime rather than compile time.

 DMD is important because this is how JAVA implements runtime


polymorphism.

 In Java, a super class reference variable can refer to a subclass object,


but it can access only those members of subclass which are inherited
form super class.

 Upcasting in Java: When Parent class reference variable refers


to Child class object, it is known as Upcasting.
Note: View versa of upcasting is referred as downcasting, but it is not allowed in java

 When an overridden method is called through a super class reference,


Java determines which version of that method to execute, based upon
the type of object being referred by the super class reference.

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

 This determination is made at runtime. Therefore, if a super class


contains a method, that is overridden by a subclass then different types
of object are referred through a super class reference, different versions
of the subclass are executed.

Following program demonstrates DMD mechanism


class A
{
void display ()
{
System.out.println("From A class display method");
}
}
class B extends A
{
void display ()
{
System.out.println("From B class display method");
}
}
class C extends A
{
void display ()
{
System.out.println("From C class display method");
}
}

class DMDTest
{
public static void main(String [] args)
{
A ref ; //super class reference
ref = new A(); //refers to A class Object
ref. display (); //calls A class display()

ref = new B(); //referes to B class Object


ref. display (); //calls B class display()

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

ref = new C (); //referes to C class Object


ref.display (); //calls C class display()
}
}

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Java Lecture-33
Topic: Abstract Method And Abstract Class

Abstract Method :
 In Inheritance, we have some methods which must be overridden by the
subclass.

 In this case, we want some way to ensure that a subclass must override all
necessary methods.

 Java's solution to this problem is an Abstract method.

 We can require that certain methods must be override in subclass by


specifying the abstract method in the super class.

 To declare an abstract method, the syntax is :-


abstract <returntype> method name (parameter list);

Properties of abstract method:


(i) Abstract method doesn't have method body ({ })

(ii) It is also called as unimplemented method.

(iii) It must be override in subclass.

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Abstract Class :
(i) Any class that contains one or more abstract method must also be
declared as an abstract.

(ii) To declare abstract class use keyword "abstract" in front of class


declaration.

(iii) We can't create an object of an abstract class but we can create the
reference.

(iv) Abstract class is also referred as "partially unimplemented class".

Program to demonstrate abstract method and abstract class:


abstract class Shape
{
double dim1, dim2;

Shape(double a, double b)
{
dim1=a;
dim2=b;
}
abstract void area();
}

class Rect extends Shape


{
Rect(double a, double b)
{
super(a,b);
}
void area()
{
System.out.println("Area of Rectangle = "+(dim1*dim2));
}

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

class Triangle extends Shape


{
Triangle(double a, double b)
{
super(a,b);
}
void area()
{
System.out.println("Area of Triangle="+(dim1*dim2/2));
}
}

class AbstractTest
{
public static void main(String [] args)
{
Shape s; //s is reference
//s=new Shape(10,10); Error, can't create object of abstract class

s=new Rect(10,9); // s referes to Ract object


s.area(); // calls Rect area()

s=new Triangle(10,8); //s referes to Triangle object


s.area(); // calss Triangle area()
}
}
Output:

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Java Lecture-34
Topic: final keyword

There are three uses of final keyword :


1) To create constant field :
If the field is declared with final modifier, then it must be initialize at the
time of declaration and we can't change the value of final field.
class math
{
final static double PI = 3.14;
}

2) To prevent method overriding :


There may be a situation, when we want to prevent method overriding
in subclass. To disallow a method from being overridden in subclass,
specify final keyword as a modifier in front of the method declaration, in
super class. Methods declared as final cannot be overridden in subclass.
For example
class A
{
final void display ()
{
System.out.println ("This is final method")
}
}
class B extends A
{
void display () //error can't override
{

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

System.out.println ("illegal");
}
}

3) To prevent Inheritance :
Sometimes we want to prevent a class from being inherited. To do this
precedes the class declaration with keyword final. Declaring a class as
final, implicitly declares all of its method as final too.
For example :
final class A
{ ----
----
----
}
class B extends A // error can't subclass A
{ ----
----
----
}

Difference between Abstract method and Final Method :


Abstract Method Final Method
i) It does not have method body i) It always have method body
ii) It is also called unimplemented ii) It is always implemented.
method.
iii) It must override in subclass. iii) It can't override within subclass.

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Difference between Final class and Abstract class :


Final class Abstract class
i) Final class cannot be inherited i) Abstract class is mean to be
inherited.
ii) Final class can be instantiated ii) Abstract class can't be instantiated.
iii) Final class cannot contain iii) Abstract class may contain final
abstract method. method.
iv) Final class is fully implemented iv) Abstract class is partially
class. unimplemented class.

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Java Lecture-35
Topic: Access Specifiers in Java

There are four types of Java access specifier


1) Private
2) Default
3) Protected
4) Public

1) Private Access Specifier


The private members are accessible only with a class.
2) Default Access Specifier
If you don’t use any access specifier, it is treated as default, by default.
The default members are accessible only within class and package.
3) Protected access specifier :
The protected members are accessible within package and outside the
package but through inheritance only.
4) Public access specifier :
The public members are accessible everywhere, it has the widest scope
among all other modifiers.
Let’s understand access specifier by simple table.
Access Within Within Outside Package Outside
Specifier class Package through Package
inheritance
private Yes No No No
default Yes Yes No No
protected Yes Yes Yes NO
public Yes Yes Yes Yes

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260

You might also like