Defination :
Inheritance can be defined as the process by which a class can acquires the properties(instance variables and methods) of another class called super class. The class which is inheriting the properties is called child class or sub class or derived class and the class whose properties are being inherited is called parent class or super class or base class. The 'extends' keyword is used to inherit the properties of a class.How to create inheritance?
class Person{
int age;
String name;
}
class Student extends Person
{
int rollNo;
public void print()
{
System.out.println(age);
System.out.println(name);
System.out.println(rollNo);
}
}
In sub class we need to specify only new variables and methods that are not a part of the super class.
No comments:
Post a Comment