Table of Contents
hide
Example : A Java program to show a simple inheritance concept.
class Parent
{
int x=50;
}
class Child extends Parent
{
int y=30;
public static void main(String[] args)
{
Child obj = new Child();
System.out.println(obj.x +" "+obj.y);
}
}
Output :
50 30
--------- OR ---------
class Parent
{
int x;
}
class Child extends Parent
{
void display()
{
x=50;
int y=30;
int z=x+y;
System.out.println(z);
}
public static void main(String[] args)
{
Child obj = new Child();
obj.display();
}
}
Output :
80
Example : A Java program to show a final class can not be inherited.
final class Parent
{
void display1()
{
System.out.println("Parent Class Method Executed");
}
}
class Child extends Parent
{
void display2()
{
System.out.println("Child Class Method Executed");
}
public static void main(String[] args)
{
Child obj = new Child();
obj.display1();
obj.display2();
}
}
Output :
F:\>javac abc.java
abc.java:8: error: cannot inherit from final Parent
class Child extends Parent
^
1 error
Example : A Java program to demonstrate the ‘this’ keyword in inheritance.
//To Represent Current Class Instance Variables
import java.io.*;
class Student
{
int rollno;
String sname;
float fee;
Student(int rollno,String sname,float fee)
{
this.rollno=rollno;
this.sname=sname;
this.fee=fee;
}
void display()
{
System.out.println(rollno +" "+ sname +" "+ fee);
}
public static void main(String args[])
{
Student stu1 = new Student(312,"Raman",12000f);
Student stu2 = new Student(217,"Sony",13500f);
stu1.display();
stu2.display();
}
}
Output:
312 Raman 12000.0
217 Sony 13500.0
------------- OR --------------
//To Represent Current Class Methods
class Example
{
int x, y, z;
void input()
{
x=10;
y=20;
}
void output()
{
//input(); same as this.input()
this.input();
z=x+y;
System.out.println(z);
}
}
class Main
{
public static void main(String args[])
{
Example obj=new Example();
obj.output();
}
}
Output:
30
------------- OR --------------
//To Represent Current Class (Default) Constructor
class Example
{
Example()
{
System.out.println("Default Constructor Executed");
}
Example(int x)
{
this(); //Calls Default Constructor Example()
System.out.println(x);
}
}
class Main
{
public static void main(String args[])
{
Example obj=new Example(100); //Calls parameterized constructor only
}
}
Output:
Default Constructor Executed
100
------------- OR --------------
//To Represent Current Class (Parameterized)7 Constructor
class Example
{
Example()
{
this(100);
System.out.println("Default Constructor Executed");
// this(100); Constructor call must be the first line code/statement in any Constructor
}
Example(int x)
{
System.out.println(x);
}
}
class Main
{
public static void main(String args[])
{
Example obj=new Example(); //calls default constructor only
}
}
Output:
100
Default Constructor Executed
------------- OR --------------
//To Pass 'this' as an Argument in the Method
class Example
{
void Call()
{
Display(this);
}
void Display(Example obj1)
{
System.out.println("Display method is Invoked by this as argument");
}
public static void main(String args[])
{
Example obj = new Example();
obj.Call();
}
}
Output:
Display method is Invoked by this as argument
Example : A Java program to show the ‘super’ keyword in inheritance.
class Parent
{
int x;
}
class Child extends Parent
{
void display()
{
super.x=50; // To represent base class (x) variable.
int x=30; // child class Local variable
int z=super.x + x;
System.out.println(z);
}
public static void main(String[] args)
{
Child obj = new Child();
obj.display();
}
}
Output :
80
--------- OR ---------
class Parent
{
int x;
}
class Child extends Parent
{
void display()
{
x=50;
int x=30;
int z=super.x+x;
int p=x+x;
System.out.println(z);
System.out.println(p);
}
public static void main(String[] args)
{
Child obj = new Child();
obj.display();
}
}
Output :
80
60
--------- OR ---------
class Parent
{
int x;
void display()
{
x=50;
System.out.println(x);
}
}
class Child extends Parent
{
int x;
void display()
{
super.display(); //parent class method
x=30;
System.out.println(x);
}
public static void main(String[] args)
{
Child obj = new Child();
obj.display();
}
}
Output :
50
30
--------- OR ---------
class Parent
{
int x;
Parent(int y)
{
x=y;
System.out.println(x);
}
}
class Child extends Parent
{
int x;
Child(int m,int n)
{
super(m); //Passing Argument to Parent Class Constructor
this.x = n; // for current/Local variable
}
void display()
{
System.out.println(super.x +" and "+ x);
}
public static void main(String[] args)
{
Child obj = new Child(50,30);
obj.display();
}
}
Output :
50
50 and 30
Example : A Java program to show Single Inheritance.
class Parent
{
void display1()
{
System.out.println("Parent Class Method Executed");
}
}
class Child extends Parent
{
void display2()
{
System.out.println("Child Class Method Executed");
}
public static void main(String[] args)
{
Child obj = new Child();
obj.display1();
obj.display2();
}
}
Output :
Parent Class Method Executed
Child Class Method Executed
Example : A Java program to show Multilevel Inheritance.
class Parent
{
int x=10;
void display1()
{
System.out.println(x);
}
}
class Child extends Parent
{
int y=20;
void display2()
{
System.out.println(y);
}
}
class Subchild extends Child
{
int z=30;
void display3()
{
System.out.println(z);
}
public static void main(String[] args)
{
Subchild obj = new Subchild();
//obj.display1(); //Error is produced
obj.display2();
obj.display3();
System.out.println();
Child obj1 = new Child();
obj1.display1();
obj1.display2();
//obj1.display3(); //Error is produced
}
}
Output :
F:\>javac abc1.java
F:\>java Subchild
20
30
10
20
Example : A Java program to show Hierarchical Inheritance.
class Parent
{
int x=10;
void display1()
{
System.out.println(x);
}
}
class Child extends Parent
{
int y=20;
void display2()
{
System.out.println(y);
}
}
class Subchild extends Parent
{
int z=30;
void display3()
{
System.out.println(z);
}
public static void main(String[] args)
{
Subchild obj1 = new Subchild();
obj1.display1();
obj1.display3();
System.out.println();
Child obj2 = new Child();
obj2.display1();
obj2.display2();
}
}
Output :
10
30
10
20
Example : A Java program to show Multiple inheritance (Not Supported in Java), but fails.
class Parent
{
void display1()
{
System.out.println("Codershelpline 01");
}
}
class Child
{
void display2()
{
System.out.println("Codershelpline 02");
}
}
class SubChild extends Parent,Child
{
public static void main(String args[])
{
SubChild obj=new SubChild();
obj.display1();
obj.display2();
}
}
Output :
F:\>javac abc1.java
abc1.java:15: error: '{' expected
class SubChild extends Parent,Child
^
1 error
F:\>
--------- OR ---------
class Parent
{
void display()
{
System.out.println("Codershelpline 01");
}
}
class Child
{
void display()
{
System.out.println("Codershelpline 02");
}
}
class SubChild extends Parent,Child
{
public static void main(String args[])
{
SubChild obj=new SubChild();
obj.display();
}
}
Output :
F:\>javac abc1.java
abc1.java:15: error: '{' expected
class SubChild extends Parent,Child
^
1 error
F:\>
0 Comments