#5 #6 Inheritance
#5 #6 Inheritance
}
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 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.