0% found this document useful (0 votes)
14 views30 pages

4 Module 3 25 09 2024

Uploaded by

Yashwant Sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views30 pages

4 Module 3 25 09 2024

Uploaded by

Yashwant Sahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Module -3

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

• On the basis of class, there can be three types


of inheritance in java: single, multilevel and
hierarchical.
• In java programming, multiple and hybrid
inheritance is supported through interface
only. We will learn about interfaces later.
• Note: Multiple inheritance is not supported
in java through class.
• /*write a java program to create a class called person which gets the details
such as SSno, name, native place. Create a subclass called projectmanager to
get the details such as projectaccount,workplace, team size.


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

The super keyword in java is a reference variable which is used


to refer immediate parent class object.
Whenever you create the instance of subclass, an instance of
parent class is created implicitly which is referred by super
reference variable.
Usage of java super Keyword:
1. super can be used to refer immediate parent class instance
variable.
2. super can be used to invoke immediate parent class method.
3. super() can be used to invoke immediate parent class
constructor.
class square
{
int a = 3;
void area()
square() {
{ //super(); // dont use super like this
System.out.println("this is parent class int area = 6*a*a;
constructor");
super.area();
}
System.out.println(area);
void area()
{ }
int area = a*a; }
System.out.println(area);
} class super1
} {
class cube extends square
public static void main(String ar[])
{
{
int a = 5;
cube() cube c = new cube();
{ c.area();
super(); // calling the constructor of super }
class }
}
Dynamic Method Dispatch
• Dynamic Method Dispatch is a process in which a
call to an overridden method is resolved at runtime
rather than compile-time.
• Another name for Dynamic Method Dispatch is
Run-time polymorphism
• 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

• When reference variable of Parent class refers


to the object of Child class, it is known as
upcasting. For example:
• Note: Runtime polymorphism can't
be achieved by data members.
Abstract class
• A class that is declared with abstract keyword,
is known as abstract class in java. It can have
abstract and non-abstract methods (method
with body).
• Before learning java abstract class, let's
understand the abstraction in java first.
• Abstraction is a process of hiding the
implementation details and showing only
functionality to the user.
• Ways to achieve Abstraction
• There are two ways to achieve abstraction in
java
• Abstract class (0 to 100%)
• Interface (100%)
abstract class
• A class that is declared as abstract is known as
abstract class. It needs to be extended and its
method implemented. It cannot be
instantiated.
• Syntax:
• abstract class <classname> {}
abstract method

• A method that is declared as abstract and


does not have implementation is known as
abstract method. Example abstract method
• abstract void printStatus();//
no body and abstract
• Example program absfaculty.java
• // program absfaculty . Java

• abstract class faculty


• {
• abstract void marks();
• }

• class student1 extends faculty


• {
• void marks()
• {
• System.out.println("I got first class!!!");
• }
• }

• class student2 extends faculty


• {
• void marks()
• {
• System.out.println("I got distinction....hurray!!!");
• }
• }

• 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??

• Whether all the methods in the abstract class to be abstract


methods…???/// (abstr.java)

• Rule: If there is any abstract method in a class, that class must be


________________________
• Rule: If you are extending any abstract class that have abstract
method, you must either provide the implementation of the
method or make this class abstract.
// sample program using abstraction – abstr.java

abstract class animal{


animal(){System.out.println("animal is a living being");}
abstract void legs();
void domestic(){System.out.println("cat,dog,parrot.....are domestic animal");}
}

class lion extends animal{


void legs(){System.out.println("lion has four legs ....");}
}
class abstr
{
public static void main(String args[])
{
animal obj = new lion();
obj.legs(); // abstract method
obj.domestic(); //non-abstract method.
}
}
Interface in JAVA
• The interface in java is a mechanism to achieve 100%
abstraction.
• There can be only abstract methods in the java
interface not method body.
• It is used to achieve abstraction and multiple
inheritance in Java (wrong program on multiple
inheritance = kk.java).
• Note: It cannot be instantiated just like abstract class.
• Note: Java Interface also represents IS-A relationship.
(ie it can be inherited)
// sample wrong program kk.java
class A
{
void show()
{
System.out.println("SDF");
}
}
class B
{
void show1()
{
System.out.println("SDFsadfasdf");
}
}

class b extends A,B


{
void show2()
{
System.out.println("SDFsdafasfs");
}
}

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)

Interface cable Interface cable


{ {
Int pins = 3; Compiler
Public static final Int pins = 3;
void usedas(); Public abstract void usedas();
} }
Understanding relationship between classes and interface

Example class to implement interface – interface1.java


Multiple inheritance in Java by interface (multiinher.java)

• If a class implements multiple interfaces


or
• an interface extends multiple interfaces i.e.
known as multiple inheritance.
• interface audio
• {
class multiinher
• void listen();
{
• }
public static void main(String ar[])
{
• interface video
movie m = new movie();
• {
m.show();
• void show();
m.listen();
• }
}
}
• class movie implements audio,video
• {
• public void listen()
• {
• System.out.println("Awesome song");
• }
• public void show()
• {
• System.out.println("Excellent scene");
• }
• }
Questions
1. Multiple inheritance is not supported through
class in java but it is possible by interface, why?

2. Can we define a class inside the interface?

3. Similar to nested class…. Is nested interface


possible??????
4. Whether Super() keyword used in interface???
Interface inheritance
• A class implements interface but one interface
extends another interface .
class Museum implements Statue // class
interface rock
implements interface
//interface1
{
{
public void broken(){System.out.println("Rocks
void broken();
broken from the mountains");}
}
public void carved(){System.out.println("broken
rocks carved as statue");}
interface Statue extends rock
// interface inheritance
public static void main(String args[]){
{
Museum m = new Museum();
void carved();
m. broken();
}
m. carved();
}
}
Program discussed in the classroom
• write a java program which contains two interfaces such as
area and volume. Declare the variables lenght and breadth
in the interface area along with a method called
area(),whereas declare a variable height in the interface
volume along with the method volume()

• extend the interface area into the interface volume.

• Create a class cuboid that implements interface volume and


calculate the volume of the cuboid by creating your own
methods if necessary
Solution for the program discussed in the
classroom
• interface area
• {
• void area();
• int length=10,breadth=10;
• }

• interface volume extends area


• {
• void volume();
• int height=10;
• }

• class cuboid implements volume


• {
• public void area()
• {
• System.out.println("ASDF");
• }
• public void volume()
• {
• int cal=length*breadth*height;
• System.out.println(cal);
• }
• }

• class mainn
• {
• public static void main(String str[])
• {
• cuboid c = new cuboid();
• c.volume();
• }
• }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy