100% found this document useful (1 vote)
425 views

CS8392 - Oop - Unit - 2 - PPT - 2 .1

This document discusses object oriented programming concepts in Java like inheritance, constructors, and types of inheritance. It defines inheritance as a mechanism where a subclass acquires properties and behaviors of a parent class. It describes single, multilevel, and hierarchical inheritance with examples. It also discusses default, parameterized, and copy constructors in Java and provides examples. Finally, it notes that multiple inheritance is not supported in Java to avoid complexity and ambiguity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
425 views

CS8392 - Oop - Unit - 2 - PPT - 2 .1

This document discusses object oriented programming concepts in Java like inheritance, constructors, and types of inheritance. It defines inheritance as a mechanism where a subclass acquires properties and behaviors of a parent class. It describes single, multilevel, and hierarchical inheritance with examples. It also discusses default, parameterized, and copy constructors in Java and provides examples. Finally, it notes that multiple inheritance is not supported in Java to avoid complexity and ambiguity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

 

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

For Code Reusability.


Terms used in Inheritance
Class: A class is a group of objects which have common properties. 

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.

a) Method with same as the class name. 

b) Does not require a return type.

c) Invoked once we create an object for the class.


Types of Java constructors

There are three types of constructors in Java:

1) Default constructor (no-argument constructor)

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.

In Java, a constructor with parameters are called parameterized constructor.

A copy constructor in a Java class is a constructor that creates an object using another object


of the same Java class.
Default constructor (no argument constructor)
Example
public class dc public class cons
{ {
  int s1,s2,s3,s4;   public static void main(String[] arg)
  dc() {
   {   dc obj = new dc();
          
System.out.println("This is Constructor }
without argument");
   }

}
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

You might also like