0% found this document useful (0 votes)
34 views8 pages

#5 #6 Inheritance

Uploaded by

nandini.qms
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views8 pages

#5 #6 Inheritance

Uploaded by

nandini.qms
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

INHERITANCE

1. Inheritance is a property of Object-Oriented Programming in which an object of one


class gets the properties and behaviors of another class. The class in which the object
belongs is known as the sub class/derived class/child class and the class from which it
gets the properties/behaviors is known as super class/base class/parent class .
2. Inheritance implements Generalization- Specialization hierarchy. It represents a parent
child relationship.
3. Parent class is created first. It is also called general base class which contains general
properties and behaviors . Depending on the requirement child classes are created. A sub
class can be created from only one base class. But from a base class more than one sub
class can be created.
4. In java , only Single Inheritance is possible in which derived class is created from only
one base class.
5. A java class can not have more than one base class. In an inheritance where a child type
is derived from more than one base types, is known as Multiple Inheritance. Multiple
Inheritance at class level is not allowed in Java. But it is allowed in the Interface level.
6. Keyword extends is used to create a child class from a base class.

public class Base


{

public class Derived extends Base


{

}
7. Inheritance is a unidirectional relationship. Direction of access always from child to the
parent. A child class can access members of a parent class but a parent class cannot
access the members of its children classes. Parent class does not know anything about the
child classes.
8. Sub classes are known as the specialized classes as they can have special properties and
behaviors that parent class does not have.
9. Single inheritance can be extended beyond one level which is known as Multilevel
inheritance.

VEHICLE
CLASS

FOUR-
WHEELER
CAR

10. Private members of parent class can not be accessed from child class but the object of
child class contains all the members of its immediate parent or ancestors. So the object of
the bottommost child class is a big object holding all the members of its parent or
ancestors.Through methods of the respective parent class, all the private instance
variables of the parent classe can be accessed.

11. Protected access specifier can be used for the instance variables of the base class so that
the child class can directly access them from any package.

,
In the above picture , Person is the general base class. Programmer, Dancer and Singer
are the sub classes of Person. Person class has general properties name and designation .It
has the general behaviors learn(),walk() and eat(). All the subclasses inherit the above
mentioned members from Person but they also have special properties and method
through which they are distinguished from each other.

12. Method Overriding—Sub class can change the definition of the method inherited from
the base class by keeping the method prototype same. This is known as method
overriding. Method overriding helps to change the nature of the behavior from parent
class to child class.It can be done if there is a requirement then only.
13. super- It is a keyword and by this a subclass can access the member of its immediate
parent class.It can be used inside any instance method.
14. super()—It is a call to the immediate base class constructor. We can pass parameter to
super() to call the immediate base class parameterized constructor. In every class default
constructor there is a by default super() as the first line of the constructor. If we want to
use super() then it should be the 1st line of the constructor of child class. super() can only
be used inside the constructor.

public class Car//general base class


{ //instance variables
private String carno,chassisno,model,company;
//default constructor
public Car()
{
carno="";
chassisno="";
model="";
company="";
}
//parameterized constructor
public Car(String carno,String chassisno,String model,String company)
{
this.carno=carno;
this.chassisno=chassisno;
this.model=model;
this.company=company;
}
//instance methods
//setter method
public void setCarno(String carno)
{
this.carno=carno;
}
//getter method
public String getCarno()
{
return carno;

}
public void display()
{
System.out.println(carno);
System.out.println(chassisno);
System.out.println(model);
System.out.println(company);
}

}
//Subclass
public class SUV extends Car
{
private double hp;
public SUV()
{ // super(); implicit super() calls the immediate base class constructor
// initialises instance variables
hp=0.0;

}
public SUV(String carno,String chassisno,String model,String company,double hp)
{
super(carno,chassisno,model,company);//explicit super() call
this.hp=hp;
}
//method overriding
public void display()
{ //call the immediate base class method
super.display();
System.out.println("Power of the Engine="+hp + " hp");
}
}
ABSTRACT CLASS

Most of the time general base class remains incomplete due to absence of the
requirements of the behaviors. Then we declare the base class as abstract base class .In
java ,a class declared with the keyword abstract is known as abstract base class. It can
contain the following-
1. Instance variables
2. Instance methods(complete)
3. Constructors
4. Abstract method- incomplete in nature and so can not be defined, declared with
abstract keyword.
5. Class variables
6. Class methods
As abstract class is incomplete in nature so object of abstract class can not be created.
But we can create reference of the class. The subclass of an abstract class is by default abstract in
nature. But we can convert it to a concrete class by defining the abstract method of the base class
by keeping the prototype same. Polymorphism is implemented when different subclasses of an
abstract base class define the abstract method differently following the requirements of the
system.
Abstract method is only the method declaration without the method definition .abstract keyword
is used to declare a method as abstract.

//abstract class
public abstract class Account
{
protected String accno;
protected double balance;
//constructor
//instance method
//abstract method
public abstract void withdraw(double amt);
//concrete method
public void deposit(double amt)
{
balance=balance+amt;
}
}//end of the class
//sub classes
public class SavingsAccount extends Account
{
//members
//constructor
public void withdraw(double amt)//concrete
{
//method definition
}
}//end of the subclass
public class CurrentAccount extends Account
{
//extra members
//constructor
public void withdraw(double amt)//concrete
{
//method definition
}
}//end of the subclass
-------------------------------------------------------------------------------------------
The reference of the abstract class can refer to its subclass object and can call the methods
declared in base class but defined in sub classes.
Account ac;// reference of the abstract base class
ac=new SavingsAccount(); // the reference is pointing to the sub class object
ac.deposit(1000); // call the method defined in Savings but inherited by both the sub classes
ac.withdraw(2000);//polymorphism- calls SavingsAccount class withdraw()
ac=new CurrentAccount(); //now the same reference points to the CurrentAccount object
ac.deposit(2000); //
ac.withdraw(100);//polymorphism-calls CurrentAccount class withdraw()
----------------------------------------------------------------
THIS DOCUMENT IS FREE FROM PLAGIARISM.

THANK YOU------
PROGRAMMING MAKES A STUDENT MORE LOGICAL
AND FIT FOR THE SOCIETY. SO DO THINK MORE
AND DO PROGRAMMING MORE.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy