Inheritance in Java
Inheritance is one of the feature of Object-Oriented Programming . Inheritance allows a class to use the properties and methods of another class. When a class "ABC" can access
the properties and method of a class "XYZ" then the "XYZ" class is called "Parent/Super" class and the "ABC" class is called "Sub/Derived" class . Syntax is
class XYZ{
// write your logic here
}
class ABC extends XYZ{
// write your logic here
}
Example :
class A
{
int x;
public void display()
{
System.out.println("Value of X is :"+x);
}
}
public class B extends A{
public static void main(String []args)
{
A obj=new A();
obj.x=10;
obj.display();
}
}
Output :
Value of X is :10
Find us :
Facebook : @apnaandroid
Google+ : Apna Java
Youtube : Android & Java Tutorial
the properties and method of a class "XYZ" then the "XYZ" class is called "Parent/Super" class and the "ABC" class is called "Sub/Derived" class . Syntax is
class XYZ{
// write your logic here
}
class ABC extends XYZ{
// write your logic here
}
Example :
class A
{
int x;
public void display()
{
System.out.println("Value of X is :"+x);
}
}
public class B extends A{
public static void main(String []args)
{
A obj=new A();
obj.x=10;
obj.display();
}
}
Output :
Value of X is :10
Find us :
Facebook : @apnaandroid
Google+ : Apna Java
Youtube : Android & Java Tutorial
Comments
Post a Comment