Java Online Compiler

Inheritance

Inheritance

Extending the Properties of one class to another class is known as inheritance.The main aim of the inheritance is reusability.In java language these inheritances are classified into following types.
  • Single or simple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance
  • Hybrid inheritance
Single Inheritance
Extending the propertis of single parent class to single child class is known as Single or simple Inheritance.
syntax:
class parent
{
..
..
..
}
class child extends parent
{
..
..
..
}
Note:here "extends" Keyword is used to extends the parent class properties to child class.

Click here to view code

/*Program for Student Details using Single Inhiritance concept*/

import java.util.*;
class Details
{
String name,location,address;
char gender;
}
class Student extends Details
{
String id;
float marks,fee;
Student()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter name");
name=s.next();
System.out.println("Enter location");
location=s.next();
System.out.println("Enter address");
address=s.next();
System.out.println("Enter gender");
gender=s.next().charAt(0);
System.out.println("Enter id");
id=s.next();
System.out.println("Enter marks");
marks=s.nextFloat();
System.out.println("Enter fee");
fee=s.nextFloat();
}
void studentDetails()
{
System.out.println("Name of the student is "+name);
System.out.println("location of the student is "+location);
System.out.println("Address of the student is "+address);
System.out.println("Gender of the student is "+gender);
System.out.println("Student id is "+id);
System.out.println("Marks of the student is "+marks);
System.out.println("Student Fee is "+fee);
}
}
class SingleInhiritance
{
public static void main(String args[])
{
Student st=new Student();
st.studentDetails();
}
}

MultiLevel Inheritance
Extending the propertis of one class to another class in a sequential order is known as Multilevel Inheritance.
syntax:
class A
{
..
..
..
}
class B extends A
{
..
..
..
}
class C extends B
{
..
..
..
}
class D extends C
{
..
..
..
}
Note:if we create object for the 'D' class we can access A,B,C,D Classes.

/*Program for Student Details using multilevel Inhiritance concept*/

import java.util.*;
class Details
{
String name,location,address;
char gender;
}
class Student extends Details
{
String id;
}
class Faculty extends Student
{
float salary;
Faculty()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter name");
name=s.next();
System.out.println("Enter location");
location=s.next();
System.out.println("Enter address");
address=s.next();
System.out.println("Enter gender");
gender=s.next().charAt(0);
System.out.println("Enter id");
id=s.next();
System.out.println("Enter Salary");
salary=s.nextFloat();
}
void facultyDetails()
{
System.out.println("Name of the Faculty is "+name);
System.out.println("location of the Faculty is "+location);
System.out.println("Address of the Faculty is "+address);
System.out.println("Gender of the Faculty is "+gender);
System.out.println("Faculty id is "+id);
System.out.println("Faculty Salary is "+salary);
}
}
class MultilevelInhiritance
{
public static void main(String args[])
{
Faculty F=new Faculty();
F.facultyDetails();
}
}

Hierarchical Inheritance
Extending the propertis of Single class to multiple child classes known as Hierarchical Inheritance
syntax:
class A
{
..
..
..
}
class B extends A
{
..
..
..
}
class C extends A
{
..
..
..
}
class D extends A
{
..
..
..
}
  
Click here to view code


/*Program for Student Details using Hirarchy Inhiritance concept*/

import java.util.*;
class Details
{
String name,location,address;
char gender;
}
class Student extends Details
{
String sid;
float marks,fee;
Student()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter name");
name=s.next();
System.out.println("Enter location");
location=s.next();
System.out.println("Enter address");
address=s.next();
System.out.println("Enter gender");
gender=s.next().charAt(0);
System.out.println("Enter sid");
sid=s.next();
System.out.println("Enter marks");
marks=s.nextFloat();
System.out.println("Enter fee");
fee=s.nextFloat();
}
void studentDetails()
{
System.out.println("Name of the student is "+name);
System.out.println("location of the student is "+location);
System.out.println("Address of the student is "+address);
System.out.println("Gender of the student is "+gender);
System.out.println("Student id is "+sid);
System.out.println("Marks of the student is "+marks);
System.out.println("Student Fee is "+fee);
}
}
class Faculty extends Details
{
String fid;
float salary;
Faculty()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter name");
name=s.next();
System.out.println("Enter location");
location=s.next();
System.out.println("Enter address");
address=s.next();
System.out.println("Enter gender");
gender=s.next().charAt(0);
System.out.println("Enter fid");
fid=s.next();
System.out.println("Enter Salary");
salary=s.nextFloat();
}
void facultyDetails()
{
System.out.println("Name of the Faculty is "+name);
System.out.println("location of the Faculty is "+location);
System.out.println("Address of the Faculty is "+address);
System.out.println("Gender of the Faculty is "+gender);
System.out.println("Faculty id is "+fid);
System.out.println("Faculty Salary is "+salary);
}
}
class HirarchyInhiritance
{
public static void main(String args[])
{
Student st=new Student();
st.studentDetails();
Faculty F=new Faculty();
F.facultyDetails();
}
}

Multiple Inheritance
Extending the propertis of Multiple parent classes to single child class known as Multiple Inheritance
syntax:
class A
{
..
..
..
}
class B
{
..
..
..
}
class C
{
..
..
..
}
class D extends A,B,C
{
..
..
..
}
Note: In Java Language multiple inheritance con't be supported at class level .To overcome this problem Interfaces are introduced.
Hybrid inheritance
Combination of above all Inheritances is known as Hybrid inheritance
syntax:
class A
{
..
..
..
}
class B extends A 
{
..
..
..
}
class C  extends D
{
..
..
..
}
class D extends B
{
..
..
..
}
Click here to view code

/*Program for Student Details using Hybrid Inhiritance concept*/

import java.util.*;
class Details
{
String name,location,address;
char gender;
}
class Faculty extends Details
{
String id;
}
class Student extends Faculty
{
float fee;
}
class EEE extends Student
{
String sub;
EEE()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter name");
name=s.next();
System.out.println("Enter location");
location=s.next();
System.out.println("Enter address");
address=s.next();
System.out.println("Enter gender");
gender=s.next().charAt(0);
System.out.println("Enter id");
id=s.next();
System.out.println("Enter fee");
fee=s.nextFloat();
System.out.println("Enter EEE subject");
sub=s.next();
}
void EEEstudentDetails()
{
System.out.println("Name of the student is "+name);
System.out.println("location of the student is "+location);
System.out.println("Address of the student is "+address);
System.out.println("Gender of the student is "+gender);
System.out.println("EEE Student id is "+id);
System.out.println("EEE Student Fee is "+fee);
System.out.println("EEE Subject is "+sub);
}
}
class ECE extends Student
{
String sub;
ECE()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter name");
name=s.next();
System.out.println("Enter location");
location=s.next();
System.out.println("Enter address");
address=s.next();
System.out.println("Enter gender");
gender=s.next().charAt(0);
System.out.println("Enter id");
id=s.next();
System.out.println("Enter fee");
fee=s.nextFloat();
System.out.println("Enter ECE subject");
sub=s.next();
}
void ECEstudentDetails()
{
System.out.println("Name of the student is "+name);
System.out.println("location of the student is "+location);
System.out.println("Address of the student is "+address);
System.out.println("Gender of the student is "+gender);
System.out.println("ECE Student id is "+id);
System.out.println("ECE Student Fee is "+fee);
System.out.println("ECE Subject is "+sub);
}
}
class HybridInhiritance
{
public static void main(String args[])
{
EEE EE=new EEE();
EE.EEEstudentDetails();
ECE EC=new ECE();
EC.ECEstudentDetails();
}
}

Abstract class and Abstract method
If any method contains only signature without any body part or implementation is knows as abstract method or incomplete method.
If any method is preceded by abstract keyword is known as abstract method.
Note:
If any class containing atleast one abstract method that class becomes abstract class. syntax:
abstract class classname
{
abstract returntype methodname(list of parameters/no parameters);
..
..
returntype methodname(list of parameters/no parameters)
{
..
..
}

..
..

}
                                    
Note:
Abstract class contains both abstract methods and concrete methods.
The main aim of the abstract method is to overcome the same method in multiple derived classes with different implementation.
Addtion Of Two Numbers Program Without Using Abstract keyword
Abstract

class Demo
{
int x,y,z;
void add()
{
x=10;
y=20;
z=x+y;
System.out.println(z);
}
}

class Addition extends Demo
{
void add()
{
x=100;
y=200;
z=x-y;
System.out.println(z);
}
}

class Addition1 extends Demo
{
void add()
{
x=1000;
y=2000;
z=x*y;
System.out.println(z);
}
}

class Main
{
public static void main(String args[])
{
Addition a=new Addition();
Addition1 aa=new Addition1();
a.add();
aa.add();
}
}

Addtion Of Two Numbers Program With Using Abstract keyword

abstract class Demo
{
int x,y,z;
abstract void add();

}
class Addition extends Demo
{
void add()
{
x=100;
y=200;
z=x-y;
System.out.println(z);
}
}

class Addition1 extends Demo
{
void add()
{
x=1000;
y=2000;
z=x*y;
System.out.println(z);
}
}


class Main
{
public static void main(String args[])
{
Addition a=new Addition();
Addition1 aa=new Addition1();
a.add();
aa.add();
}
}

Creating object for abstract class or interface

abstract class Demo
{
int x,y,z;
abstract void add();
void show()
{
	System.out.println("welcome");
	
}

}
class Addition extends Demo
{
void add()
{
x=100;
y=200;
z=x-y;
System.out.println(z);
}}

class Addition1 extends Demo
{
void add()
{
x=1000;
y=2000;
z=x*y;
System.out.println(z);
}}


class Main
{
public static void main(String args[])
{
	Demo d=new Demo();
	d.show();

}
}
output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Cannot instantiate the type Demo Note 1: we con't create a object for abstract class or interface directly.
Note 2: If you want create object for the abstract class or interface with the help of derived class name Syntax: parentclass object =new childclassname(); Demo d=new Addition1();

Creating object for abstract class or interface

abstract class Demo
{
int x,y,z;
abstract void add();
void show()
{
	System.out.println("Welcome");
	
}

}
class Addition extends Demo
{
void add()
{
x=100;
y=200;
z=x-y;
System.out.println(z);
}}

class Addition1 extends Demo
{
void add()
{
x=1000;
y=2000;
z=x*y;
System.out.println(z);
}}
class Main
{
public static void main(String args[])
{
	Demo d=new Addition1();
	d.show();

}
}

output:
Welcome


Interface
Interface is similar to class.It is used to achieve the multiple inheritance in java language.
Interface is replacement of abstract class.
interface InterfaceName
{
list of public static final variables;
list of abstract methods();
}
Every interface can be identify with interface keyword and represented by user defined name.

Defination of interface:

Interface is similar to class,Which is collection of public static final variables and abstract methods.
The main advantege with interface is common reusability that means these methods can be reused many number of classes with separate implementation part.
Interface always contains variable initilizations and abstract methods as shown below.
interface iface
{
int x=10; -> the signature is public static final int x=10;
void show(); -> abstract void show();
void disp();-> abstract void disp();
}
The properties of interface can be reuse any class with the help of implements keyword.
class classname implements  InterfaceName1,InterfaceName,...
{
..
..
}
Every abstract method of interface should be overridden in the implemented class otherwise that class becomes abstract class.
Note:
  • If any abstract method is not overridden the derived class then that derived class also becomes abstract class(every abstract method should be overridden).
  • The main disadvantage in abstract class is it does not support multiple inheritance,so to overcome this problem we use interface.
Interface Prograam

interface Addition
{
 void add();
}
interface Substraction
{
 void sub();
}
class Demo implements Addition,Substraction 
{
int x,y,z;
public void add()
{
x=100;
y=200;
z=x+y;
System.out.println(z);
}
public void sub()
{
x=100;
y=200;
z=x-y;
System.out.println(z);
}
void mul()
{
x=100;
y=200;
z=x*y;
System.out.println(z);
}

}


class Main
{
public static void main(String args[])
{
Demo d=new Demo();
d.add();
d.mul();
}
}