Professional Documents
Culture Documents
U02 - Inheritance, Interface & Packages (30.07.2018)
U02 - Inheritance, Interface & Packages (30.07.2018)
J Dhyaneswaran
AP - IT
Object Oriented Principles
• Object Oriented Principles are core to JAVA
• 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
Shape
length : int
• Polymorphism can be of two types draw(length) : void
• 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
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: Employee
salary : float
Extends
Extends
Class: Debugger
Debugging: float
Example (1)
Employee
Programmer Tester
Debugger
Example (1) cont.
Employee
Programmer Tester
Debugger
InheritExample(main class)
Method Overriding
• When subclass or child class has the same method as declared in the parent class
• 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)
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
• 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{
}}
class Dog extends Animal{
String color="black";
void printColor( ) {
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();
}
}