Inheritance- Inheritance is the Reusability of class.
Inheritance defines the usabilty of class members in another class.
Inheritance Working Methodology-
Class Sharing.
Types of Class-
1. Base Class.
2. Derived Class.
Rules for inheritance-
1. Always the last class object is created.
2. main class can not be counted in no. of classes, it's a driver class.
3. All base class members can be accessed in derived class but not the vice versa.
Example-
import java.util.Scanner;
class First
{
public void display()
{
System.out.println("This is base class");
}
}
class Second extends First
{
public void show()
{
System.out.println("This is derived class");
}
}
public class inheri
{// driver class [class body]
public static void main(String args[])
{// main body
Second s1 = new Second();
s1.display();
s1.show();
}
}
Comments
Post a Comment