Session 20 Inheritance
Session 20 Inheritance
• class B extends A {
• private int y=20; //not
• }
• class C extends B {
• private int z=30; //not
• }
• class Child
• {
• Public void methodTwo()
• { }
• }
Dr. Naveen Kumar, Associate Professor, AIIT, AUP
Dr. Naveen Kumar, Associate
Professor, AIIT, AUP
Interpretation
• 1) Whatever members the parent has by default available to the
child but whatever members the child has by default not available to
the parent.
• Hence on the child reference we can call both parent
and child class methods.
• Any class can extends only one class at a time and can't
extends more than one class simultaneously hence java
won't provide support for multiple inheritance.
• If our class extends any other class then our class is not
direct child class of object, It is indirect child class of
object , which forms multilevel inheritance.
System.out.println(name);
System.out.println(age);
System.out.println(salary);
}
}
//Base class
class Base {
public static void main(String args[])
{
Sunstar s = new Employee();
s.printInfo();
} Dr. Naveen Kumar, Associate
} Professor, AIIT, AUP
2. Abstract Class having
constructor, data member, and
methods
• Elements abstract class can have
• data member
• abstract method
• method body (non-abstract method)
• constructor
• main() method.
void Learn(){
System.out.println("Preparing Right Now!");
}
}
class Demo {
public static void main(String[] args) {
Subject x=new IT();
x.syllabus();
x.Learn();
}
}
class Main {
public static void main(String args[])
{
//Base b = new Base();
// but We can have references of Base type.
Base b = new Derived();
b.fun();
}
}
•}
•class Demo {
•public static void main(String args[])
•{
•Base b = new Derived();
•b.fun();
•}
•}
Dr. Naveen Kumar, Associate
Professor, AIIT, AUP
• an abstract class instantiation is not
possible.
abstract class AbstractClass {
public static void main(String args[])
{
// Trying to create an object
AbstractClass gfg = new AbstractClass();
}
}
•class Demo {
•public static void main(String[] args)
•{
•// FirstChild f=new FirstChild();
•// f.m1();
A class can contain concrete (with The interface cannot contain concrete
implementation) methods (with implementation) methods.
class can inherit from only one A class can implement multiple
Inheritance abstract class. interfaces.
Can have member variables (final, Variables are implicitly public, static,
Variables non-final, static, non-static). and final (constants).
// Driver Code
public static void main(String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(t.a);
}
}
//Interface 2
//Extending the above interface
interface Interface1 extends API {
// Abstract method
void display();
}
//Interface 3
//Extending the above interface
interface Interface2 extends API {
// Abstract method
void print();
}
Dr. Naveen Kumar, Associate
Professor, AIIT, AUP
class TestClass implements Interface1, Interface2 {
// Overriding the abstract method from Interface1
public void display()
{
System.out.println("Display from Interface1");
}
// Overriding the abstract method from Interface2
public void print()
{
System.out.println("Print from Interface2");
}
//Level 2
abstract class Dev1 implements Bank {
public void deposit()
{
System.out.println("Your deposit Amount :" + 100);
}
}
//Level 4
class Demo {
public static void main(String[] args)
{
Dev3 d = new Dev3();
d.account();
d.loan();
d.deposit();
d.withdraw();
}
} Dr. Naveen Kumar, Associate
Professor, AIIT, AUP
Super keyword
• Characteristics of Super Keyword in J
ava
• Use of super keyword in Java
• 1. Use of super with Variables
• 2. Use of super with Methods
• 3. Use of super with constructors
• Advantages of Using Java Super Key
word
void display()
{
// print maxSpeed of base class (vehicle)
System.out.println("Maximum Speed: "
+ super.maxSpeed);
}
}
//Driver Program
class Test {
public static void main(String[] args)
{
Car small = new Car();
small.display();
}
} Dr. Naveen Kumar, Associate
Professor, AIIT, AUP
class Person {
void message()
{
System.out.println("This is person class\n");
}
}
class Student extends Person {
void message()
{
System.out.println("This is student class");
}
// Note that display() is only in Student class
void display()
{
message(); //call current class message() method
super.message(); //call parent class message() method
}
}
//Driver Program
class Test {
public static void main(String args[])
{
Student s = new Student();
s.display(); // calling display() of Student
} Dr. Naveen Kumar, Associate
} Professor, AIIT, AUP
class Person {
Person()
{
System.out.println("Person class Constructor");
}
}
//subclass Student extending the Person class
class Student extends Person {
Student()
{
// invoke or call parent class constructor
super();
System.out.println("Student class Constructor");
}
}
//Driver Program
class Test {
public static void main(String[] args)
{
Student s = new Student();
}
Dr. Naveen Kumar, Associate
} Professor, AIIT, AUP
Advantages of Super
Keyword
• Enables reuse of code:
• Using the super keyword allows subclasses to inherit functionality from
their parent classes, which promotes the reuse of code and reduces
duplication.
• Supports polymorphism:
• Because subclasses can override methods and access fields from their
parent classes using super, polymorphism is possible. This allows for more
flexible and extensible code.
• Provides access to parent class behaviour:
• Subclasses can access and use methods and fields defined in their parent
classes through the super keyword, which allows them to take advantage
of existing behaviour without having to reimplement it.
• Allows for customization of behaviour:
• By overriding methods and using super to call the parent implementation,
subclasses can customize and extend the behaviour of their parent classes.
• Facilitates abstraction and encapsulation:
• The use of super promotes encapsulation and abstraction by allowing
subclasses to focus on their behaviour while relying on the parent class to
handle lower-level details.
Dr. Naveen Kumar, Associate
Professor, AIIT, AUP
Anonymous Class