OCJP

Inheritance in Java

One class can acquire the properties of another class, that is called Inheritance. Here meaning is not one class can use the properties of another class!!.

Here mechanism of Parent class and Child class is developed. Properties of Parent class is derived by Child class (Except Private Properties). Default properties can be used in same package only.

class Parent{
	private void privatePro(){
	System.out.println("Private Function Of Parent");
	}
	
	protected void protectedPro(){
	System.out.println("Protected Property of Parent");
	}
	
	void defaultPro(){
	System.out.println("Default Properties of Parent Class");
	}
	
	public void publicPro(){
	System.out.println("Public Properties of Parent Class");
	}
}

class Child extends Parent{
	
	public void ownPro(){
	System.out.println("Own Properties");
	}

}

class InheritanceDemo1{

	public static void main(String []a){
	
	Child childObj= new Child(); //Object of Child is Created
	
	//childObj.privatePro(); //Error Can not use private properties of Parent class
	
	childObj.protectedPro();
	childObj.defaultPro(); //In Different Package can not be used default 
	childObj.publicPro();
	childObj.ownPro();
	
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *


× How can I help you?