CS8392 - Oop - Unit - 2 - PPT - 2 .1
CS8392 - Oop - Unit - 2 - PPT - 2 .1
CS 8392
OBJECT
ORIENTED
PROGRAMMING
VA S A N T H A K U M A R V
AP/CSE
Inheritance
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.
Why use inheritance in java
For Method Overriding (runtime polymorphism ).
Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a
derived class, extended class, or child class.
Super Class/Parent Class: Superclass is the class from where a subclass inherits the features.
It is also called a base class or a parent class.
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.
The syntax of Java Inheritance
class Subclass-name extends Superclass-name
{
//methods and fields
}
class Employee
{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
Example public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
} }
Types of inheritance in java
Types of inheritance in java
Single Inheritance
When one subclass inherits the features of one superclass, this would be the case of Single
inheritance.
Multi-level Inheritance
In the case of multi-level inheritance, a subclass that is inheriting one parent class will also
act as the base class for another class.
Hierarchical Inheritance
In the case of Hierarchical Inheritance, there is one base class for multiple subclasses.
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
Single void bark(){System.out.println("barking...");}
Inheritance
}
class TestInheritance{
Example public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat(); }}
Multilevel Inheritance Example
class Animal{ class TestInheritance2{
void eat(){System.out.println("eating...");} public static void main(String args[]){
} BabyDog d=new BabyDog();
class Dog extends Animal{ d.weep();
void bark(){System.out.println("barking...");} d.bark();
} d.eat();
class BabyDog extends Dog{ }}
void weep(){System.out.println("weeping...");} }
Hierarchical Inheritance Example
class Animal{ class Cat extends Animal{
void meow()
void eat(){System.out.println("eating...");} {System.out.println("meowing...");}
}
}
class TestInheritance2{
class Dog extends Animal{
public static void main(String args[]){
void bark(){System.out.println("barking...");} Cat c=new Cat();
c.meow();
} c.eat();
BabyDog d=new BabyDog();
class BabyDog extends Dog{
d.eat();
void weep(){System.out.println("weeping...");} } }}
Types of inheritance NOT supported by
java
Types of inheritance NOT supported by java
To reduce the complexity and simplify the language, multiple inheritance is not supported in
java.
Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes.
If A and B classes have the same method and you call it from child class object, there will be
ambiguity to call the method of A or B class.
Since compile-time errors are better than runtime errors, Java renders compile-time error if
you inherit 2 classes. So whether you have same method or different, there will be compile
time error.
Constructors in Java
In Java, a constructor is a block of codes similar to the method. It is called when an instance
of the class is created.
At the time of calling constructor, memory for the object is allocated in the memory.
2) Parameterized constructor
3) Copy constructor
Types of Java constructors
In Java, a constructor is said to be default constructor if it does not have any parameter.
Default constructor can be either user defined or provided by JVM.
}
Parameterized constructor Example
public class dc public class cons
{ {
int s1,s2,s3,s4; public static void main(String[] arg)
dc(int num1,int num2) {
{
s1=num1; dc obj1 = new dc(10,20);
s2=num2;
System.out.println("This is Constructor with }
2 arguments" + s1 + ","+ s2);
} }
}
Copy constructor Example
public class dc{
int s1,s2,s3,s4; public class cons
dc(int a) { {
s1=a; public static void main(String[] arg)
System.out.println("This is Constructor {
with 1 argument" + s1); dc obj2 = new dc(40);
}
dc(dc c){ dc obj3 = new dc(obj1);
s3=c.s1;
System.out.println("This is }}
Copy Constructor with 1 argument" + s3 );
}}
THANK YOU
VA S A N T H A K U M A R V
AP/CSE