ProjectName : App_Inheritance_1
package test;
public class A
{
public int a;
public static int b;
public void m1()
{
[Link]("=====ParentClass Instance method m1()====");
[Link]("The value a:"+a);
[Link]("The value b:"+b);
}
public static void m2()
{
[Link]("=====ParentClass Static method m2()====");
//[Link]("The value a:"+a);
[Link]("The value b:"+b);
}
}
package test;
public class B extends A
{
public int x;
public static int y;
public void m3()
{
[Link]("=====ChildClass Instance method m3()=====");
[Link]("The value x:"+x);
[Link]("The value y:"+y);
}
public static void m4()
{
[Link]("=====ChildClass Static method m4()=====");
//[Link]("The value x:"+x);
[Link]("The value y:"+y);
}
}
maccess : [Link](MainClass)
package maccess;
import [Link].*;
import test.*;
public class DemoInheritance1
{
public static void main(String[] args)
{
Scanner s = new Scanner([Link]);
B ob = new B();//Child_class_Object(Extention_Object)
[Link]("Enter the value for a:");
ob.a = [Link]();
[Link]("Enter the value for b:");
B.b = [Link]();
[Link]("Enter the value for x:");
ob.x = [Link]();
[Link]("Enter the value for y:");
B.y = [Link]();
ob.m1();
B.m2();
ob.m3();
B.m4();
[Link]();
}
}
o/p:
Enter the value for a:
11
Enter the value for b:
12
Enter the value for x:
13
Enter the value for y:
14
=====ParentClass Instance method m1()====
The value a:11
The value b:12
=====ParentClass Static method m2()====
The value b:12
=====ChildClass Instance method m3()=====
The value x:13
The value y:14
=====ChildClass Static method m4()=====
The value y:14
Multilevel Inheritance:
Person → Student → CollegeStudent
Person → Base class
Student → Inherits from Person
CollegeStudent → Inherits from Student
package test;
public class Person {
public String name;
public int age;
public void displayPerson() {
[Link]("---- Person Details ----");
[Link]("Name: " + name);
[Link]("Age: " + age);
package test;
public class Student extends Person {
public int rollNo;
public String course;
public void displayStudent() {
[Link]("---- Student Details ----");
[Link]("Roll No: " + rollNo);
[Link]("Course: " + course);
package test;
public class CollegeStudent extends Student {
public String collegeName;
public double fees;
public void displayCollegeStudent() {
[Link]("---- College Student Details ----");
[Link]("College Name: " + collegeName);
[Link]("Fees: " + fees);
package maccess;
import test.*;
public class DemoMultiLevelInheritance {
public static void main(String[] args) {
CollegeStudent cs = new CollegeStudent();
[Link] = "Rahul";
[Link] = 21;
[Link] = 102;
[Link] = "Computer Science";
[Link] = "ABC Engineering College";
[Link] = 55000.0;
// Display data from all levels of inheritance
[Link]();
[Link]();
[Link]();
Output:
---- Person Details ----
Name: Rahul
Age: 21
---- Student Details ----
Roll No: 102
Course: Computer Science
---- College Student Details ----
College Name: ABC Engineering College
Fees: 55000.0
Hierarchical Inheritance.
ProjectName: App_Inheritance_3_Assignment_Solution
test: [Link]
package test;
public class Student
{
public int studentId;
public String name;
public double examFee;
public String displayDetails()
{
return "";
}
public double payFee(double amt)
{
return 0.0;
}
}
test : [Link]
package test;
public class DayScholar extends Student
{
public double transportFee;
public String displayDetails()
{
return "StudentId:"+studentId+"\nStuName:"+name+"\
nExamFee:"+examFee+
"\nTransportFee:"+transportFee;
}
public double payFee(double amt)
{
return amt-(examFee+transportFee);
}
}
test : [Link]
package test;
public class Hosteller extends Student
{
public double hostelFee;
public String displayDetails()
{
return "StudentId:"+studentId+"\nStuName:"+name+"\
nExamFee:"+examFee+
"\nHostelFee:"+hostelFee;
}
public double payFee(double amt)
{
return amt-(examFee+hostelFee);
}
}
maccess : [Link](MainClass)
package maccess;
import [Link].*;
import test.*;
public class StudentTester {
public static void main(String[] args) {
Scanner s = new Scanner([Link]);
[Link]("=====Choice=====");
[Link]("\[Link]"
+ "\n\[Link]");
[Link]("Enter Your Choice:");
int choice = [Link]([Link]());
switch(choice)
case 1:
DayScholar ds = new DayScholar();
[Link]("Enter the StudentId:");
[Link] = [Link]([Link]());
[Link]("Enter the StudentName:");
[Link] = [Link]();
[Link]("Enter the ExamFee:");
[Link] = [Link]();
[Link]("Enter the TransportFee:");
[Link] = [Link]();
[Link]("Enter the total amt paid by Student:");
double amt1 = [Link]();
[Link]([Link]());
[Link]("Amt to be paid :"+[Link](amt1));
break;
case 2:
Hosteller hs = new Hosteller();
[Link]("Enter the StudentId:");
[Link] = [Link]([Link]());
[Link]("Enter the StudentName:");
[Link] = [Link]();
[Link]("Enter the ExamFee:");
[Link] = [Link]();
[Link]("Enter the HostelFee:");
[Link] = [Link]();
[Link]("Enter the total amt paid by Student:");
double amt2 = [Link]();
[Link]([Link]());
[Link]("Amt to be paid :"+[Link](amt2));
break;
default:
[Link]("Your Choice is Invalid....");
}//end of switch
[Link]();
o/p:
=====Choice=====
[Link]
[Link]
Enter Your Choice:
Enter the StudentId:
213
Enter the StudentName:
Ram
Enter the ExamFee:
21000
Enter the HostelFee:
83000
Enter the total amt paid by Student:
50000
StudentId:213
StuName:Ram
ExamFee:21000.0
HostelFee:83000.0
Amt to be paid :-54000.0
==================================================================
Note:
=>Multiple Inheritance process using classes not available in Java, because which
generate replication of programming components and raises ambiguity.
=>The ambiguity state aplications will generate wrong results.
=>In Java,we use Interfaces to perform Multiple Inheritance process.
-----------------------------------------------------------------------------
*imp
Interfaces in Java:
=>Interface is collection of Varaibles,abstract methods and Concrete methods
from Java8-version onwards.
(Upto Java7-version Interfaces cannot hold Concrete methods)
define abstract methods?
=>The methods which are declared without method_body are known as abstarct methods.
structure of abstract methods:
abstract return type method name(para_list);
define concrete methods?
=>The methods which are declared with method body are known as Concrete methods
or Constructed methods
structure of concrete methods:
return type method name(para_list)
//method body
Coding rules of Interface:
Rule-1 : we use 'interface' keyword to declare interfaces.
syntax:
interface Interface name
//Interface body
Rule-2 : The programming components which are declared within the interface are
automatically 'public'
Note:
=>The programming components which are declared within the class without
any access modifier are automatically 'default'
Rule-3 : Interfaces can be declared with both primitive datatype variables and
NonPrimitive datatype variables.
Rule-4 : The variables which are declared within the interface are automatically
'static and final variables'
Note:
(i)static variables in interface will get the memory within the Interface
and can be accessed with Interface_name
(ii)final variables must be initialized with values and once initialized
cannot be modified.
(final variables are also known as Constant variables and secured
variables)
Rule-5 : The methods which are declared within the interface are automatically
NonStatic abstract methods.
(There is no concept of static abstract methods)
Rule-6 : Interfaces can be declared with any number of abstarct methods without
any restriction
Rule-7 : we cannot instantiate Interfaces,which means we cannot create object for
Interfaces directly.
Rule-8 : Interfaces are implemented to classes using 'implements' keyword and the
classes are known as Implementation Classes.
Rule-9 : These implementation classes must implement all abstract methods,which
means construct body for all abstract methods.
Rule-10 : Implementation classes can also be declared with NonImplemented methods.
Rule-11 : Interfaces cannot be declared with Constructors and Blocks.
Diagram:
Ex: define Multiple Inheritance?
=>The process of taking features(members) from more than one class at-a-time is
known as Multiple Inheritance process.
Multiple Inheritance design models using Interfaces:
Design Model-1 : Extracting features from more than one interface into a class.
(Class implements from more than one Interface)
Diagram:
ProjectName : App_Interface_3
test : [Link]
package test;
public interface ITest1
{
public abstract void m1(int x);
}
test : [Link]
package test;
public interface ITest2
{
public abstract void m2(int y);
}
test : [Link]
package test;
public class IClass implements ITest1,ITest2
{
public void m1(int x)
{
[Link]("----ITest1 Implemented method
m1(x)----");
[Link]("The value x:"+x);
}
public void m2(int y)
{
[Link]("----ITest2 Implemented method
m2(y)-----");
[Link]("The value y:"+y);
}
}
maccess : [Link](MainClass)
package maccess;
import test.*;
public class DemoInterface3
{
public static void main(String[] args)
{
IClass ob = new IClass();//Implementation Object
ob.m1(11);
ob.m2(12);
}
o/p:
----ITest1 Implemented method m1(x)----
The value x:11
----ITest2 Implemented method m2(y)-----
The value y:12
----------------------------------------
Design Model-2 : Extracting features from one class and any number of Interfaces
into a class
(Class extends from one class and can be implemented from any number
of Interfaces)
ProjectName : App_Interface_4
test : [Link]
package test;
public class PClass
{
public void m1(int a)
{
[Link]("-----PClass method m1(a)------");
[Link]("The value a:"+a);
}
}
test : [Link]
package test;
public interface ITest
{
public abstract void m2(int b);
}
test : [Link]
package test;
public class SubClass extends PClass implements ITest
{
public void m2(int b)
{
[Link]("----ITest Implemented method m2(b)-----");
[Link]("The value b:"+b);
}
}
maccess : [Link](MainClass)
package maccess;
import [Link];
public class DemoInterface4 {
public static void main(String[] args) {
SubClass ob = new SubClass();//Implementation Object
ob.m1(11);
ob.m2(12);
}
}
o/p:
-----PClass method m1(a)------
The value a:11
----ITest Implemented method m2(b)-----
The value b:12
-----------------------------------------------------------
Design Model-3 : Extracting features from more than one interafce into a Interface.
(Interface extends from more than one Interface)
Diagram:
ProjectName : App_Interface_5
test : [Link]
package test;
public interface ITest1
{
public abstract void m1(int a);
}
test : [Link]
package test;
public interface ITest2
{
public abstract void m2(int b);
}
test : [Link]
package test;
public interface ITest3 extends ITest1,ITest2
{
public abstract void m3(int c);
}
test : [Link]
package test;
public class IClass implements ITest3
{
public void m1(int a)
{
[Link]("----ITest1 Implemented method
m1(a)----");
[Link]("The value a:"+a);
}
public void m2(int b)
{
[Link]("----ITest2 Implemented method
m2(b)----");
[Link]("The value b:"+b);
}
public void m3(int c)
{
[Link]("----ITest3 Implemented method
m3(c)----");
[Link]("The value c:"+c);
}
}
maccess : [Link](MainClass)
package maccess;
import [Link];
public class DemoInterface5
{
public static void main(String[] args)
{
IClass ob = new IClass();//Implementation Object
ob.m1(11);
ob.m2(12);
ob.m3(13);
}
}
o/p:
----ITest1 Implemented method m1(a)----
The value a:11
----ITest2 Implemented method m2(b)----
The value b:12
----ITest3 Implemented method m3(c)----
The value c:13
=================================================================
*imp
Concrete methods in Interface:(Java8-version new feature)
=>From Java8-version(2014) onwards the interfaces can be declared with Concrete
methods.
=>The following Concrete methods can be declared in Interfaces:
[Link] Concrete methods(Java8-version)
[Link] Concrete methods(Java8-version)
[Link] Concrete methods(Java9-version-2017)
[Link] Concrete methods(Java8-version):
=>The concrete methods in interfaces which are declared with 'static' keyword
are known as static concrete methods.
=>These static concrete methods will get the memory within the interface and
can be accessed with interface name directly.
[Link] Concrete methods(Java8-version):
=>The concrete methods in interfaces which are declared with 'default' keyword
are known as default Concrete methods
=>These default Concrete methods will get the memory within the Implementation
object and can be accessed with Implementation_Objct_name.
[Link] Concrete methods(Java9-version-2017):
=>The Concrete methods in Interfaces which are declared with private keyword are
known as private Concrete methods.
=>These private Concrete methods are categorized into two types:
(i)static private Concrete methods
(ii)NonStatic private Concrete methods
=>private Concrete methods are accessed by the NonPrivate Concrete methods of
same interface.