Sunday, May 6, 2018
JAVA INHERITANCE
Java inheritance example
This is Java inheritance example.we have three classes in java named as- Child Class
- Parent Class
- Test Class
The child class contain a string variable name=burhan. The parent class also have two variable and a method(function) to display name. In test class we created and object of child class to access the name of person in child class.
Note: The keyword extends is used for inheritance. It refers the parent of that class. By doing this the child class can access object of it's parent class such as name or any method. You can see in Test class we created an object of child class because child can have it's own characteristics as well as his parent characteristics. So the C.show() means you need to write object name and than specify which method or variable name you want to access.
This is very easy java inheritance example.
Class name: Child
public class Child extends Parent
{
String name="burhan";
}
Class name: Parent
public class Parent
{
private
String pname="Ali";
String city="multan";
void show()
{
System.out.println(pname);
}
}
Class name: Test
public class Test {
private String n="Burhan";
public static void main(String[] args)
{
Child C=new Child();
C.show();
}
}
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment