Inheritance
Q)What is inheritance?
=>Inheritance is one of the three object oriented features.
=>Acquiring features from the existing entities is nothing but inheritance.
=>Inheritance is a mechanism of creating new classes from the already existing
classes using is-a relationship.
=>Already existing classes are known as parent classes(Super classes).
=>Newly created classes are known as child classes(Sub classess).
=>Sub classes inherit properties and behaviour from the super classes. I.e. Sub classes inherit instance
variables and instance methods from the super classes.
=>Inheritance fecilitates code reusability.
Q)What is the syntax to create a sub class?
class <sub class name> extends <super class name>{
}
For eg.
class Vehicle{
}//super class
class Car extends Vehicle{
}//super class
=>In the above example, Vehicle is the super class(parent class) and Car is the
sub class(child class).
=>Car class inherits all the properties and behaviour from the super class Vehicle.
Q)What is the output of the following Program?
class Vehicle{
protected void move() {
System.out.println("Vehicle is moving");
}
}
class Car{
}
class InheritanceApplication{
public static void main(String args[]) {
Car c=new Car();
c.move();
}
}
Ans:- The above program causes compilation error.
=>without defining move() method in Car class, main method is calling
move() method on the Car class object.
Q)What is the output of the folloing program?
class Vehicle{
protected void move() {
System.out.println("Vehicle is moving");
}
}
class Car extends Vehicle{
Q)What is inheritance?
=>Inheritance is one of the three object oriented features.
=>Acquiring features from the existing entities is nothing but inheritance.
=>Inheritance is a mechanism of creating new classes from the already existing
classes using is-a relationship.
=>Already existing classes are known as parent classes(Super classes).
=>Newly created classes are known as child classes(Sub classess).
=>Sub classes inherit properties and behaviour from the super classes. I.e. Sub classes inherit instance
variables and instance methods from the super classes.
=>Inheritance fecilitates code reusability.
Q)What is the syntax to create a sub class?
class <sub class name> extends <super class name>{
}
For eg.
class Vehicle{
}//super class
class Car extends Vehicle{
}//super class
=>In the above example, Vehicle is the super class(parent class) and Car is the
sub class(child class).
=>Car class inherits all the properties and behaviour from the super class Vehicle.
Q)What is the output of the following Program?
class Vehicle{
protected void move() {
System.out.println("Vehicle is moving");
}
}
class Car{
}
class InheritanceApplication{
public static void main(String args[]) {
Car c=new Car();
c.move();
}
}
Ans:- The above program causes compilation error.
=>without defining move() method in Car class, main method is calling
move() method on the Car class object.
Q)What is the output of the folloing program?
class Vehicle{
protected void move() {
System.out.println("Vehicle is moving");
}
}
class Car extends Vehicle{