4 Module 3 25 09 2024
4 Module 3 25 09 2024
Inheritance /Abstract/Interface
Dr. A. Anitha
Associate Prof.
SITE
Fall Sem 2023-2024
Inheritance
• The idea behind inheritance in java is that you can create
new classes that are built upon existing classes. When you
inherit from an existing class, you can reuse methods and
fields of parent class, and you can add new methods and
fields also.
• Inheritance represents the IS-A relationship, also known
as parent-child relationship.
• Why use inheritance in java
• For Method Overriding (so runtime polymorphism can be
achieved).
• For Code Reusability.
• Syntax of Java Inheritance
• class Subclass-name extends Superclass-name
• {
• //methods and fields
• }
• The extends keyword indicates that you are making
a new class that derives from an existing class. The
meaning of "extends" is to increase the
functionality.
• In the terminology of Java, a class which is inherited
is called parent or super class and the new class is
called child or subclass.
Types of inheritance in java
•
void display()
Again create one more class called father and get the detail as marital status,
if the marital status as "married", get the details of number of children else if
martial status as "Single", display "Awaiting for an AWESOME person".
{
System.out.println(ssn + " "+name+"
• Report the list for a single person using multi level inheritance in java.*/
"+place+" "+ts + " " +acc+ " "
•
•
import java.util.*;
class person // super class
+workplace);
•
•
{ }
int ssn;
• String name, place; }//subclass
• void getdetails()
• {
•
•
System.out.println("Enter the details of the person");
Scanner s = new Scanner(System.in);
class sing
• ssn = s.nextInt(); {
• name = s.next();
• place = s.next(); public static void main(String a[])
•
•
}
} //superclass {
• class pm extends person //subclass
pm p =new pm();
• { p.getdata();
• String acc,workplace;
• int ts; p.getdetails();
•
•
void getdata()
{
p.display();
•
•
System.out.println("Enter the details of the project manager");
Scanner s = new Scanner(System.in);
}
• ts = s.nextInt(); }
• acc = s.next();
• workplace = s.next();
• }
Method overriding
If subclass (child class) has the same method as declared in the
parent class, it is known as method overriding in java.
Usage of Java Method Overriding
• Method overriding is used to provide specific
implementation of a method that is already provided by its
super class.
• Method overriding is used for runtime polymorphism
Rules for Java Method Overriding
1. Method must have same name as in the parent class
2. Method must have same parameter as in the parent class.
3. Must be IS-A relationship (inheritance).
Example program – overriding1.java
class square
{ class overriding1
int a = 3;
void area()
{
{
int area = a*a;
public static void
System.out.println(area); main(String ar[])
}
} {
class cube extends square
{
cube c = new
int a = 5;
void area()
cube();
{ c.area();
int area = 6*a*a;
super.area(); }
System.out.println(area);
}
}
}
super keyword in java
• class absfaculty
• {
• public static void main(String args[])
• {
• faculty f2 = new faculty(); // faculty is abstract: it cannot be instantiated....
• faculty f = new student1();
• faculty f1 = new student2();
• f.marks();
• f1.marks();
• }
• }
• CAN abstract class can have data member, abstract method,
method body, constructor and main() method??????????
• Can the parent class and the child class both be declared abstract??
class kk
{
public static void main(String a[])
{
b b1 = new b();
b1.show2();
}
}
How the compiler
takes the interface
• The java compiler adds public and abstract
keywords before the interface method.
More, it adds public, static and final
keywords before data members (Very
important) (Example program interface1.java)
• class mainn
• {
• public static void main(String str[])
• {
• cuboid c = new cuboid();
• c.volume();
• }
• }