Sunteți pe pagina 1din 27

Unit - 2

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.
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;
System.out.println(area);
public static void
}
}
main(String ar[])
class cube extends square
{ {
int a = 5;
void area() cube c = new
{
int area = 6*a*a; cube();
super.area();
System.out.println(area); c.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
{
void area()
int a = 3;
{
square()
//super(); // dont use super like this
{
int area = 6*a*a;
System.out.println("this is parent class
constructor"); super.area();
} System.out.println(area);
void area() }
{ }
int area = a*a;
System.out.println(area); class super1
} {
} public static void main(String ar[])
class cube extends square {
{ cube c = new cube();
int a = 5; c.area();
cube() }
{ }
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 abstr
act
Example program absfaculty.java
CAN abstract class can have data member, abstract
method, method body, constructor and main()
method??????????

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
{
void listen(); class multiinher
} {
public static void main(String ar[])
interface video
{
{
void show(); movie m = new movie();
} m.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. Si ilar to ested class. Is ested i terface


possible??????
Interface inheritance
A class implements interface but one interface
extends another interface .
class Museum implements Statue // class
interface rock //interface1
implements interface
{
{
void broken();
public void
}
broken(){System.out.println("Rocks
broken from the mountains");}
interface Statue extends rock //
public void
interface inheritance
carved(){System.out.println("broken
{
rocks carved as statue");}
void carved();
}
public static void main(String args[]){
Museum m = new Museum();
m. broken();
m. carved();
}
}
Packages in JAVA

S-ar putea să vă placă și