single inheritance in java with example programs
Inheritance
Single inheritance
single inheritance in java program with output
Java single inheritance Sample program
single inheritance in java example program
class Main1
{
public static void main(String args[])
{
ChildExample obj=new ChildExample();
obj.display();
System.out.println("Age is :"+obj.age);
obj.display1();
System.out.println("Age is :"+obj.age1);
}
}
class ParentExample
{
int age=60;
void display()
{
System.out.println("This is parent class example");
}
}
class ChildExample extends ParentExample
{
int age1=18;
void display1()
{
System.out.println("This is child class example");
}}
advantages of inheritance in java with example
Here the child class is inherited,in order to inherit the child class
to the parent class we have using the keyword extends
By using child class objects we can access variables and methods of parent class,
and there is no need to create separate objects for parent class.
This is one of the advantages of the inheritance concept.
In the absence of inheritance we have to create two objects
one is child and another one is parent but in order to access
variables and methods of child and parent class Just created
the child class object only.
Comments
Post a Comment