You are on page 1of 30

UNIT II - INHERITANCE, PACKAGES AND INTERFACES

J Dhyaneswaran
AP - IT
Object Oriented Principles
• Object Oriented Principles are core to JAVA

• All programs contain two integral parts code & data

• Programming paradigms are of two types

1. Process Oriented – Code Acting On Data


• Function acting on variables in ‘C’ Program

2. Object Oriented – DATA CONTROL ACCESS TO DATA


• Data acts as controlling entity of program execution

• Objects invoking methods of its class in ‘JAVA’ Program


Data Abstraction & Encapsulation
• Abstraction means representing essential features
Shape
• without providing details of variables declaration and method definitions length : int
• All data members are provided hierarchical abstractions draw(length) : void

• Data Encapsulation means wrapping up of data and methods into a single unit

• Variables of class cannot be accessed from another class or outside class directly

• Variables of that class can be accessed by its member methods


Class Box
length : int
• Hiding the data’s direct access is called ‘data hiding’
draw(length) : void

• Data Abstraction & Encapsulation are provided by the class


Inheritance
• Inheritance is the process by which new class acquires property of already existing class

• subclass inherits all of the attributes of all of its ancestors

• Inheritance support hierarchical classification

• Inheritance supports idea of reusability

Inheritance is made available by using class


Polymorphism
• Feature that allows one methods to take up more than one form

• One Operation can exhibit different behaviors in different instances

Shape
length : int
• Polymorphism can be of two types draw(length) : void

1. Compile Time Polymorphism – Method Overloading


2. Run Time Polymorphism – Method Overriding Circle
Box
length : int length : int
draw(length) : void draw(length) : void
Inheritance
• Mechanism in which new class acquires all the attributes and behaviors of existing class

• Inheritance is a concept of Object Oriented Programming

• Creation of new class based on existing classes

• Reuse methods and fields of parent class

• Additional methods and fields can be created in child class


Features of Inheritance
• Inheritance provides following important features

1. Method Over-riding (Runtime Polymorphism)

2. Code Reusability (add existing attributes & behaviors)


Terminology
• Class
• A class is a template for group of objects which have common attributes & behaviors
• It is a template or blueprint from which objects are created

• Sub Class/Child Class


• Subclass is a class which inherits from existing class
• It is also called a derived class, extended class or child class

• Super Class/Parent Class


• Superclass is the class where a subclass inherits the features
• It is also called a base class or a parent class

• Reusability
• Reuse the fields and methods of the existing class when you create a new class
• You can use the same fields and methods already defined in previous class
Inheritance
Syntax

class Subclass-name extends Superclass-name  
{  
   //methods and fields  

• extends keyword indicates that you are making a new class that derives from an existing
class
Inheritance Types
• Inheritance types possible in JAVA Environment are
1. Single Inheritance
2. Multi Level
3. Hierarchical
4. Multiple
5. Hybrid

• Multiple Inheritance is not directly supported in JAVA


Single Inheritance Example
Class: Employee

salary : float

Extends

Class: Programmer

Increment: float
Multi Level Inheritance
Class: Employee

salary : float
Extends

Class: Programmer

Increment: float

Extends

Class: Programmer

Increment: float
Hierarchical Inheritance
Class: Employee

salary : float

Extends Extends

Class: Programmer Class: Tester

Increment: float testduration: float


Hybrid Inheritance
• Combination of any of the above three approaches form hybrid inheritance

Class: Employee

salary : float

Extends

Class: Programmer Class: Tester

Increment: float testduration: float

Extends

Class: Debugger

Debugging: float
Example (1)
Employee

Programmer Tester

Debugger
Example (1) cont.
Employee

Programmer Tester

Debugger

InheritExample(main class)
Method Overriding

• Is a scenario in Inheritance and is Runtime polymorphism

• When subclass or child class has the same method as declared in the parent class

Method Overriding Rules


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 in an inheritance

• Sub Class Method overrides definition of Inherited method and gets executed
Method Overriding Example Method Overriding

Inheritance of run( )
Definition of void run( ) In
In
Class MethodOverExp
Class MethodOverExp
with Inheritance
No Method Overloading Method Overriding

1) Method overloading increase the readability of the Method overriding provides specific implementation of
program. the method that is already in super class.

2) Method overloading is performed within class. Method overriding occurs in two classes that have
inheritance (IS-A relationship)

3) In case of method overloading, parameter must be In case of method overriding, parameter must be


different. same.

4) Method overloading is the example of Compile Time Method overriding is the example of Run Time
Polymorphism. Polymorphism.

5) method overloading can't be performed by changing Return type must be same or covariant in method
return type of the method only.  overriding.
Final Keyword In Java
• final keyword in java is used to provide restrictions
• final keyword can be used in following context
1. Variable – Constant variables
2. Methods – Avoids Method Overriding
3. Classes – Avoids Inheritance

• final variables are declared using final keyword


• Final variables are also constant variables

Example: (next slide)


Final Keyword with variable
class Bike9{  
 final int speedlimit=90; //final variable  
 void run(){  
  speedlimit=400;  
 }  
 public static void main(String args[]){  
 Bike9 obj=new  Bike9();  
 obj.run();  
 }  
} //end of class  

Output:Compile Time Error Variable is declared final


Final Keyword with Methods
class Bike{
final void run(){ // final method cannot be overridden
System.out.println("running");
}
}
class Honda extends Bike{
void run(){ // causes compilation error
System.out.println("running safely with 100kmph");
} // cannot be overridden
public static void main(String args[]){
Honda honda= new Honda();
honda.run();
}
}
Final Keyword with classes
final class Bike{
void run() {
System.out.println(“running”);
}
}  
class Honda1 extends Bike{   // Error: final class cannot be inherited
  void run(){
System.out.println("running safely with 100kmph");
}  
public static void main(String args[]){  
Honda1 honda= new Honda();  
honda.run();  
}  
}   // Output : Compilation Error
super keyword
• is a reference variable which refers immediate parent class object

• It refers to Base class reference variable when sub class objects are created

super usage
1. super - refer immediate parent class instance variable
2. super.methodName( ) - invoke immediate parent class method
3. super() - invoke immediate parent class constructor
Base class instance variable
class Animal{ class TestSuper1{

String color="white"; public static void main(String args[]){

Dog d=new Dog();


}
d.printColor();

}}
class Dog extends Animal{

String color="black";

void printColor( ) {

System.out.println(color); //prints color of Dog class

System.out.println(super.color); //print color of Animal class


}
}
Invoke parent class method
class Animal{ void work(){
void eat() super.eat(); Invokes Animal
{ System.out.println("eating..."); bark(); Class Method eat()
} }
} }

class Dog extends Animal{


void eat(){ class TestSuper2{
System.out.println("eating bread..."); public static void main(String args[]){
} Dog d=new Dog();
d.work();
}
void bark(){
}
System.out.println("barking...");
}
super invoke parent class constructor.
class Animal{ class TestSuper3{
Animal(){ public static void main(String args[]){
System.out.println("animal is created"); Dog d=new Dog();
} }
} }

class Dog extends Animal{


Dog(){
super(); // invokes parent class constructor
System.out.println("dog is created");
}
}
Abstract class
• Class that is declared with abstract keyword
• Abstract classes can have
• Data Members
• Abstract Method Declaration - it enforces method definition
• Method Definition
• Can implement an interface or extend a class
• Cannot instantiate or create objects

Syntax
abstract class Employee{
//Data members
//Abstract Method declaration
//Method definition
}
Abstract Class Example
abstract class Bike{
abstract void run(); // Method declared abstract
}
class Honda4 extends Bike{
void run(){ // Method defined in inherited class
System.out.println("running safely..");
}
public static void main(String args[]){
Bike obj = new Honda4(); // Reference variable
obj.run();
}
}

You might also like