0% found this document useful (0 votes)
92 views

UNIT-3 (Oops Using Java)

This Document gives the information about Inheritance, Types of Inheritance, Super Keyword, Abstract Classes, Method overriding, Interfaces and Packages.

Uploaded by

g mahesh
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

UNIT-3 (Oops Using Java)

This Document gives the information about Inheritance, Types of Inheritance, Super Keyword, Abstract Classes, Method overriding, Interfaces and Packages.

Uploaded by

g mahesh
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 59

UNIT-3

• Inheritance
• Types of Inheritance
• Super Keyword
• Abstract Classes
• Method Over ridding
• Interfaces
• Packages Presented by
G. Mahesh
Assistant Professor
Bapatla Engineering College
INHERITANCE
• Inheritance is the mechanism of deriving new
class from old one, old class is known as super
class and new class is known as sub class.
• The sub class inherits all of its instances
variables and methods defined by the super
class. Thus we can say that subclass is
specialized version of super class.
Cont…..
The sub class derives super class using the keyword
extends.
Syntax:
Class base
{
Data members;
Member functions;
}
Class derived extends base
{
}
Cont…..
• When the derived class extends base, the
derived class takes the properties from base
and we can access that properties of base
through the object of derived.
derived obj=new derived();
obj.data members;
obj.member functions();
Cont…..
Benefits of Java’s Inheritance
• Reusability of code
• Code Sharing
Classes

Super class(Base Class) Subclass(Child Class)

It is a class from which other It is a class that inherits some


classes can be derived. or all members from super
class.
Types of Inheritance in Java

• Single Inheritance –Single inheritance is a


mechanism of deriving only one child class
from only one base class
Cont….
• Multilevel Inheritance: Multilevel inheritance
is the mechanism of deriving a sub class class
from another sub class.
Cont…
• Hierarchical Inheritance –Hierarchical
inheritance is a mechanism of deriving two or
more child classes from only one base class.
i.e. one class is extended by many subclasses.
It is one- to-many relationship.
Cont…..
• Multiple Inheritance – Multiple inheritance is
a mechanism of deriving only one child class
from two or more base classes. i.e. two or
more base classes are extended by only one
sub classe. It is many- to-one relationship.
Cont….
• Hybrid Inheritance – Hybrid inheritance is a
combination of above inheritances.

B C
Reusability: As the name specifies,
reusability is a mechanism which facilitates
you to 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 the previous class.

The meaning of "extends" is to increase the


functionality.
class Animal{  
void eat()
{System.out.println("eating...");}  
}  
class Dog extends Animal
{  
void bark(){System.out.println("barking...");}  
}  
class TestInheritance
{  
public static void main(String args[]){  
Dog d=new Dog();  
d.bark();  
d.eat();  
}}  
class Animal{  
void eat()
{System.out.println("eating...");}  
}  
class Dog extends Animal
{  
void bark(){System.out.println("barking...");}  
}  
class BabyDog extends Dog
{  
void weep(){System.out.println("weeping...");}  
}  
class TestInheritance2
{  
public static void main(String args[]){  
BabyDog d=new BabyDog();  
d.weep();  
d.bark();  
d.eat();  
}}  
class Animal
{  
void eat(){System.out.println("eating...");}  
}  
class Dog extends Animal
{  
void bark(){System.out.println("barking...");}  
}  
class Cat extends Animal
{  
void meow(){System.out.println("meowing...");}  
}  
class TestInheritance3
{  
public static void main(String args[])
{  
Cat c=new Cat();  
c.meow();  
c.eat();    
}}  
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

super can be used to refer immediate parent class


instance variable.

super can be used to invoke immediate parent


class method.

super() can be used to invoke immediate parent


class constructor.
1) super is used to refer immediate parent
class instance variable.

We can use super keyword to access the


data member or field of parent class. It
is used if parent class and child class
have same fields.
class Animal
{  
String color="white";  
}  
class Dog extends Animal
{  
String color="black";  
void printColor()
{  
System.out.println(color);//prints color of Dog class  
System.out.println(super.color);
}  }  
class TestSuper1{  
public static void main(String args[]){  
Dog d=new Dog();  
d.printColor();  
}}  
In the above example, Animal and Dog
both classes have a common property
color. If we print color property, it will
print the color of current class by
default. To access the parent property,
we need to use super keyword.
2) super can be used to invoke parent class
method

The super keyword can also be used to invoke


parent class method. It should be used if
subclass contains the same method as parent
class. In other words, it is used if method is
overridden.
class Animal
{  
void eat(){System.out.println("eating...");}  
}  
class Dog extends Animal
{  
void eat(){System.out.println("eating bread...");}  
void bark(){System.out.println("barking...");}  
void work()
{  
super.eat();  
bark();  
}  }  
class TestSuper2{  
public static void main(String args[]){  
Dog d=new Dog();  
d.work();  
}}  
In the above example Animal and Dog both
classes have eat() method if we call eat()
method from Dog class, it will call the eat()
method of Dog class by default because
priority is given to local.

To call the parent class method, we need to


use super keyword.
3) super is used to invoke parent class
constructor.

The super keyword can also be used to


invoke the parent class constructor.
class Animal
{  
Animal()
{System.out.println("animal is created");}  
}  
class Dog extends Animal
{  
Dog(){  
super();  
System.out.println("dog is created");  
}  }  
class TestSuper3{  
public static void main(String args[]){  
Dog d=new Dog();  
}}  
Abstract class in Java

A class which is declared as abstract is known as an abstract


class.
It can have abstract and non-abstract methods. It needs to be
extended and its method implemented.
It cannot be instantiated.
Points to Remember
 An abstract class must be declared with an abstract
keyword.
It can have abstract and non-abstract methods.
It cannot be instantiated.
It can have constructors and static methods also.
It can have final methods which will force the subclass not to
change the body of the method.
Abstraction in Java
Abstraction is a process of hiding the
implementation details and showing
only functionality to the user.

Syntax of abstract class


abstract class A{}  
Abstract Method in Java
A method which is declared as abstract and does
not have implementation is known as an abstract
method.

Example of abstract method


abstract void printStatus();
//no method body and abstract  
Example of Abstract class that has an abstract method
In this example, Bike is an abstract class that contains only
one abstract method run. Its implementation is provided by
the Honda class.
abstract class Bike
{  
  abstract void run();  
}  
class Honda4 extends Bike
{  
void run(){System.out.println("running safely");
}  
public static void main(String args[]){  
 Bike obj = new Honda4();  
 obj.run();  
}  }  
Method Overriding in Java

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 the specific
implementation of a method which is already provided by
its superclass.
•Method overriding is used for runtime polymorphism
Rules for Java Method Overriding

The method must have the same name as in


the parent class

The method must have the same parameter as


in the parent class.

There must be an inheritance


//Java Program to illustrate the use of Java Metho
d Overriding    
class Vehicle
{    
  void run()
{System.out.println("Vehicle is running");}  
}    
class Bike2 extends Vehicle
{    
  void run()
{System.out.println("Bike is running safely");}  
  
  public static void main(String args[]){  
  Bike2 obj = new Bike2();//creating object  
  obj.run();//calling method  
  }  
}  
class Bank
{  
int  getRateOfInterest(){return 0;}  
}  
//Creating child classes.  
class SBI extends Bank
{  
int  getRateOfInterest(){return 8;}  
}  
  class ICICI extends Bank
{  
Int  getRateOfInterest(){return 7;}  
}  
class AXIS extends Bank{  
int  getRateOfInterest(){return 9;}  
}  
//Test class to create objects and call the methods
  
class Test2
{  
public static void main(String args[])
{  
SBI s=new SBI();  
ICICI i=new ICICI();  
AXIS a=new AXIS();  
System.out.println("SBI Rate of Interest: "+s.getRa
teOfInterest());  
System.out.println("ICICI Rate of Interest: "+i.getR
ateOfInterest());  
System.out.println("AXIS Rate of Interest: "+a.getR
ateOfInterest());  
}  }  
Interface in Java

•The interface in Java is a mechanism to


achieve 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.

•An interface in Java is a blueprint of a class. It has


static constants and abstract methods.
Why use Java interface?

• It is used to achieve abstraction.

• By interface, we can support the


functionality of multiple inheritance.
How to declare an interface?

•An interface is declared by using the interface keyword.

•It provides total abstraction; means all the methods in an


interface are declared with the empty body, and all the
fields are public, static and final by default.

•A class that implements an interface must implement all


the methods declared in the interface.
Syntax:

interface <interface name>
{  
      
  // declare constant fields  
 //declare methods that abstract
    // by default.  
}  
Internal addition by the compiler

• The Java compiler adds public and abstract


keywords before the interface method. Moreover,
it adds public, static and final keywords before
data members.

• In other words, Interface fields are public, static


and final by default, and the methods are public
and abstract.
    
The relationship between classes and interfaces

•A class extends another class, an interface extends another


interface, but a class implements an interface.
    
interface printable{  
void print();  
}  
class A6 implements printable{  
public void print()
{System.out.println("Hello");}  
  
public static void main(String args[]){  
A6 obj = new A6();  
obj.print();  
 }  

/Interface declaration: by first user  
interface Drawable{  
void draw();  
}  
//Implementation: by second user  
class Rectangle implements Drawable{  
public void draw(){System.out.println("drawing rectangle");}  
}  
class Circle implements Drawable{  
public void draw(){System.out.println("drawing circle");}  
}  
//Using interface: by third user  
class TestInterface1{  
public static void main(String args[]){  
Drawable d=new Circle();
d.draw();  
}}  
Multiple inheritance in Java by interface

If a class implements multiple interfaces, or an


interface extends multiple interfaces, it is known as
multiple inheritance.
interface Printable
{  
void print();  
}  
interface Showable
{  
void show();  
}  
class A7 implements Printable,Showable
{  
public void print(){System.out.println("Hello");}  
public void show(){System.out.println("Welcome");}  
  
public static void main(String args[]){  
A7 obj = new A7();  
obj.print();  
obj.show();  
 }  
interface Printable{  
void print();  
}  
interface Showable{  
void print();  
}  
  
class TestInterface3 implements Printable, Showabl
e{  
public void print(){System.out.println("Hello");}  
public static void main(String args[]){  
TestInterface3 obj = new TestInterface3();  
obj.print();  
 }  
}  
Java Package
• A java package is a group of similar types of
classes, interfaces and sub-packages.

•Package in java can be categorized in two form,


built-in package and user-defined package.

•There are many built-in packages such as java,


lang, awt, etc.
Advantage of Java Package

1)Java package is used to categorize the


classes and interfaces so that they can be easily
maintained.

2) Java package provides access protection.

3) Java package removes naming collision.


//save as Simple.java  
package mypack;  
public class Simple{  
 public static void main(String args[]){  
    System.out.println("Welcome to pack
age");  
   }  
}  
How to run java package program
How to access package from another
package?

There are different ways to access the


package from outside the package.
 import package.*;
import package.classname;
1) Using packagename.*

 If you use package.* then all the classes


and interfaces of this package will be
accessible but not sub packages.

 The import keyword is used to make the


classes and interface of another package
accessible to the current package.
Example of package that import the
packagename.*
//save by A.java  
package pack;  
public class A{  
  public void msg()
{System.out.println("Hello");}  
}  
//save by B.java  
package mypack;  
import pack.*;  
  
class B{  
  public static void main(String args[]){  
   A obj = new A();  
   obj.msg();  
  }  
}  
2) Using packagename.classname

If you import package.classname then only


declared class of this package will be
accessible.
//save by A.java  
  
package pack;  
public class A{  
  public void msg()
{System.out.println("Hello");}  
}  
//save by B.java  
package mypack;  
import pack.A;  
  
class B{  
  public static void main(String args[]){  
   A obj = new A();  
   obj.msg();  
  }  
}  

You might also like