0% found this document useful (0 votes)
31 views12 pages

Advance Programming Class

Inheritance in Java allows one class to acquire properties and behaviors of another class. This creates a parent-child relationship between the classes, where the child inherits from the parent. There are different types of inheritance such as single, multilevel, hierarchical, and hybrid. Method overriding and overloading allow subclasses to provide their own implementation of methods defined in the parent class. The super keyword refers to the parent class and is used to access members of the parent class from the child class.

Uploaded by

Tween Channel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
31 views12 pages

Advance Programming Class

Inheritance in Java allows one class to acquire properties and behaviors of another class. This creates a parent-child relationship between the classes, where the child inherits from the parent. There are different types of inheritance such as single, multilevel, hierarchical, and hybrid. Method overriding and overloading allow subclasses to provide their own implementation of methods defined in the parent class. The super keyword refers to the parent class and is used to access members of the parent class from the child class.

Uploaded by

Tween Channel
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 12

Inheritance in Java

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. 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 the parent class. Moreover, you can add new methods and fields in your current class also.

Inheritance represents the IS-A relationship which is also known as a parent-child relationship.


Syntax:
class Subclass-name extends Superclass-name  
{  
   //methods and fields  
}  

Types of inheritance in java:


On the basis of class, there can be three types of inheritance in java:
1. Single
2. multilevel
3. hierarchical.
multiple and hybrid inheritance is supported through interface only.
4. Multiple
5. Hybird

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Single Inheritance
When a class inherits another class, it is known as a single inheritance.
class Subclass-name extends Superclass-name  
{  
   //methods and fields  
}  
Example: TestInheritance.java
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();  
}}  

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Multilevel Inheritance
When there is a chain of inheritance, it is known as multilevel inheritance. As you
can see in the example given below, BabyDog class inherits the Dog class which
again inherits the Animal class, so there is a multilevel inheritance.
Example: TestInheritance2.java
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();  
}}  

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Hierarchical Inheritance
When two or more classes inherits a single class, it is known
as hierarchical inheritance. In the example given below, Dog and
Cat classes inherits the Animal class, so there is hierarchical
inheritance.
Example: TestInheritance3.java
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();  
}}

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Multiple Inheritance
Java does not support multiple inheritance. This means that a class
cannot extend more than one class. Therefore, following is illegal.
public class extends Animal,Manual{}
However, a class can implement one or more interfaces,
which has helped Java get rid of the impossibility of
multiple inheritance.
Example: Tester.java
interface Event { public void start();}
interface Sports { public void play();}
interface Hockey extends Sports, Event{
public void show();}
public class Tester{
public static void main(String[] args){
Hockey hockey = new Hockey() {
public void start() {System.out.println("Start Event"); }
public void play() { System.out.println("Play Sports.");}
public void show() {System.out.println("Show Hockey.");
} }; hockey.start(); hockey.play(); hockey.show(); } }

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Hybrid Inheritance
In the following Java program, we have achieved the hybrid inheritance by
implementing the combination of single and multiple inheritance (through
interfaces).
Example:Child.java
class HumanBody  {  
public void displayHuman()  {  
System.out.println("Method defined inside HumanBody class");  }  }  
interface Male  {  
public void show();  }  
interface Female  {  
public void show();  }   
public class Child extends HumanBody implements Male, Female  {  
public void show()  {  
System.out.println("Implementation of show() method defined in interfaces Male and Femal
e");  }    
public void displayChild()  {  
System.out.println("Method defined inside Child class");  }   
public static void main(String args[])  {  
Child obj = new Child();  
System.out.println("Implementation of Hybrid Inheritance in Java");  
obj.show();  
obj.displayChild();  }  }  

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Method Overloading:
If a class has multiple methods having same name but different in # Method Overloading: changing data type of arguments:
parameters, it is known as Method Overloading. Example: TestOverloading2.java
Different ways to overload the method class Adder{  
There are two ways to overload the method in java static int add(int a, int b){return a+b;}  
1. By changing number of arguments static double add(double a, double b){return a+b;}  
2. By changing the data type }  
Example (By changing number of arguments) TestOverloading1.java class TestOverloading2{  
ass Adder{   public static void main(String[] args){  
static int add(int a,int b){return a+b;}   System.out.println(Adder.add(11,11));  
static int add(int a,int b,int c){return a+b+c;}   System.out.println(Adder.add(12.3,12.6));  
}  
}}   
class TestOverloading1{  
public static void main(String[] args){  
System.out.println(Adder.add(11,11));  
System.out.println(Adder.add(11,11,11));  

}}   

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Method Overriding:
If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding in Java.
# Example2: Test2.java

In other words, If a subclass provides the specific implementation of the method that has class Bank{  
been declared by one of its parent class, it is known as method overriding.
int getRateOfInterest(){return 0;}  
Usage of Java Method Overriding
}  //Creating child classes.  
• Method overriding is used to provide the specific implementation of a method which is
already provided by its superclass. class SBI extends Bank{  

• Method overriding is used for runtime polymorphism int getRateOfInterest(){return 8;}  }  

Rules for Java Method Overriding: class ICICI extends Bank{  


1. The method must have the same name as in the parent class int getRateOfInterest(){return 7;}  }  
2. The method must have the same parameter as in the parent class. class AXIS extends Bank{  
3. There must be an IS-A relationship (inheritance). int getRateOfInterest(){return 9;}  
Example: Bike2.java }  //Test class to create objects and call the methods  
//Java Program to illustrate the use of Java Method Overriding   class Test2{  
class Vehicle{  //defining a method   public static void main(String args[]){  
  void run(){System.out.println("Vehicle is running");}  
SBI s=new SBI();  
}  //Creating a child class  
ICICI i=new ICICI();  
class Bike2 extends Vehicle{  //defining the same method as in the parent class  
AXIS a=new AXIS();  
  void run(){System.out.println("Bike is running safely");}  
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());  
  public static void main(String args[]){  
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());  
  Bike2 obj = new Bike2();//creating object  
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());  }  } 
  obj.run();//calling method  }  } 

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Super Keyword:
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.

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Super Keyword:
1. super is used to refer immediate parent class instance variable: 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
We can use super keyword to access the data member or field of parent class. It is if subclass contains the same method as parent class. In other words, it is used if
used if parent class and child class have same fields. method is overridden.

Example: TestSuper1.java Example: TestSuper2.java

class Animal{   class Animal{  

String color="white";   void eat(){System.out.println("eating...");}  

}   }  

class Dog extends Animal{   class Dog extends Animal{  

String color="black";   void eat(){System.out.println("eating bread...");}  

void printColor(){   void bark(){System.out.println("barking...");}  

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

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

}   bark();  

}   }  

class TestSuper1{   }  

public static void main(String args[]){   class TestSuper2{  

Dog d=new Dog();   public static void main(String args[]){  

d.printColor();   Dog d=new Dog();  

}}   d.work();  
}}  
BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING
Super Keyword:
1. super is used to invoke parent class constructor.
The super keyword can also be used to invoke the parent class constructor. Let's see
a simple example:

Example: TestSuper3.java
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();  
}}  

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING


Polymorphism in java:
Polymorphism in Java is a concept by which we can perform a single action in
different ways. Polymorphism is derived from 2 Greek words: poly and morphs. The
word "poly" means many and "morphs" means forms. So polymorphism means
many forms.
There are two types of polymorphism in Java: compile-time polymorphism and
runtime polymorphism. We can perform polymorphism in java by method
overloading and method overriding.

Let us look at an example.


public interface Vegetarian{}

public class Animal{}

public class Deer extends Animal implements Vegetarian{}

Now, the Deer class is considered to be polymorphic since this has


multiple inheritance. Following are true for the above examples −
 A Deer IS-A Animal
 A Deer IS-A Vegetarian
 A Deer IS-A Deer
 A Deer IS-A Object

BCA 6th SEMESTAR | ADVANCE JAVA PROGRAMMING

You might also like