Inheritance in Java

Inheritance is an object-oriented programming technique. If you need a class similar to previous one with new additional features, java allows you to use the members of the existing class and add some more features to the new class. It is done by inheritance.
The class which you derive is super class, base class or parent class. The class which is derived is sub class, derived class, child class.
How to create Derived class,
Syntax for creating derived class,
class sub_class_name extens base_class_name


Class Subject1
public class Subject1 {
int mark1;

Subject1() {
System.out.println("Subject1 Constructor");
void showmark1()
System.out.println("Subject1 marks");
System.out.println("Subject1 Marks: "+mark1);

Class Subject2
public class Subject2 extends Subject1{
int mark2;
System.out.println("Subject2 Constructor");
void showmark2()
System.out.println("Subject2 marks");
System.out.println("Subject2 Marks: "+mark2);

Class Total
public class total {

public static void main(String[] args) {
// TODO Auto-generated method stub
Subject1 s1 = new Subject1();
Subject2 s2 = new Subject2();
System.out.println("Total Marks");
s1.mark1 = 80;
s2.mark1 = 100;
s2.mark2 = 100;
s1.mark1 = 200;
s2.mark2 = 200;

Subject1 Constructor
Subject1 Constructor
Subject2 Constructor
Total Marks
Subject1 marks
Subject1 Marks: 80
Subject1 marks
Subject1 Marks: 100
Subject2 marks
Subject2 Marks: 200

Leave a Reply