Abc Java
Abc Java
Of
DIPLOMA ENGINEERING
In
Computer Engineering
BY:-
Mr.P.S.Bhandare.
SVERI’S
PANDHARPUR 2023-24
CERTIFICATE
The project report entitled “Inheritance & it’s types.”
Submitted by:
1. Tanishka Umesh Karande
2. Samiksha Annasaheb Bhosale
3. Swapnaja Samir Kumbhar
4. Vaishnavi Santosh Chaudhari
Examiner Principal
Place: Pandharpur
Date:
Annexure II
Evolution Sheet for Micro Project
Academic Year :- 2023-24 Name of Faculty :-Mr.P. S. Bhandare.
Course :- Computer Engineering Course Code :- CO4I
Subject :-JAVA Programming. Subject Code :- 22412
th
Semester :- 4 Scheme :- I
Title of “Inheritance & it’s types in JAVA.”
Project : -
COs addressed by the Micro Project:
CO 1 Develop Programs using Object Oriented Methodology in JAVA .
(b) Unit Outcomes in 1. Apply the identified type of inheritance for the given
Cognitive domain: programming problem.
1. Follow safety practices.
(c) Outcomes in 2. Practice good housekeeping.
Affective Domain: 3. Demonstrate working as a leader/a team member.
4. Follow ethical practices.
Marks out
Marks out
of 6 for
of 4 for Total marks
Roll No. Name of Student performanc
performance in out of 10
e in group
oral/Presentation
activity
16 Tanishka Umesh Karande
34 Samiksha Annasaheb Bhosale
36 Swapnaja Samir Kumbhar
61 Vaishnavi Santosh Chaudhari
Name &
Signature of Mr. P. S. Bhandare. Signature:
Faculty
ACKNOWLEDGEMENT
I take this opportunity to express my sincere thanks and
deep sense of gratitude to my guide, mr. P. S. Bhandare sir for his
constant support, motivation, valuable guidance and immense
help during the entire course of this work. Without him constant
encouragement, timely advice and valuable discussion, it would
have been difficult in completing this work. I would also like to
acknowledge Computer Engineering department who provided
me the facilities for completion of the project. We are thankful to
him for sharing his experienced in research field with me and
providing constant motivation during entire project work.
Name of Student:-
1. Single Inheritance
2. Multi-level Inheritance
3. Hierarchical Inheritance
4. Hybrid Inheritance
Syntax:
Now, to inherit a class we need to use ‘extends’ keyword. In the below example, class Son is the
child class and class Mom is the parent class. The class Son is inheriting the properties and methods
of Mom class.
Let’s see a small program and understand how it works. In this example, we
have a base class Teacher and a subclass HadoopTeacher. Since class
HadoopTeacher extends the properties from the base class, we need not
declare these properties and method in the subclass.
class Teacher{
String designation = "Teacher";
String collegeName = "Edureka";
void does(){
System.out.println("Teaching");
}
}
public class HadoopTeacher extends Teacher{
String mainSubject = "Spark";
public static void main(String args[]){
HadoopTeacher obj = new HadoopTeacher();
System.out.println(obj.collegeName);
System.out.println(obj.designation);
System.out.println(obj.mainSubject);
obj.does();
}
}
Output:
Edureka
Teacher
Spark
Teaching
Now let’s move further and see the various types of Inheritance supported
by Java.
Here, Class A is your parent class and Class B is your child class which
inherits the properties and behavior of the parent class. A similar concept is
represented in the below code:
class Animal{
void eat(){System.out.println(“eating”);}
}
class Dog extends Animal{
void bark(){System.out.println(“barking”);}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}
}
2. Multi-level Inheritance
When a class is derived from a class which is also derived from another
class, i.e. a class having more than one parent class but at different levels,
such type of inheritance is called Multilevel Inheritance.
If we talk about the flowchart, class B inherits the properties and behavior of
class A and class C inherits the properties of class B. Here A is the parent
class for B and class B is the parent class for C. So in this case class C
implicitly inherits the properties and methods of class A along with Class B.
That’s what is multilevel inheritance.
class Animal{
void eat(){System.out.println(“eating…”);}
}
class Dog extends Animal{
void bark(){System.out.println(“barking…”);}
}
class Puppy extends Dog{
void weep(){System.out.println(“weeping…”);}
}
class TestInheritance2{
public static void main(String args[]){
Puppy d=new Puppy();
d.weep();
d.bark();
d.eat();
}
}
3. Hierarchical Inheritance
When a class has more than one child classes (subclasses) or in other words,
more than one child classes have the same parent class, then such kind of
inheritance is known as hierarchical.
In the above flowchart, Class B and C are the child classes which are
inheriting from the parent class i.e Class A.
class Animal{
void eat(){System.out.println(“eating…”);}
}
class Dog extends Animal{
void bark(){System.out.println(“barking…”);}
}
class Cat extends Animal{
void meow(){System.out.println(“meowing…”);}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
}
}
4. Hybrid Inheritance
Now that we know what is Inheritance and its various types, let’s move
further and see some of the important rules that should be considered while
inheriting class.
Rules of Inheritance in Java
Multiple inheritance refers to the process where one child class tries to
extend more than one parent class. In the above illustration, Class A is a
parent class for Class B and C, which are further extended by class D. This is
results in Diamond Problem. Why? Say you have a method show() in both
the classes B and C, but with different functionalities. When Class D extends
class B and C, it automatically inherits the characteristics of B and C
including the method show(). Now, when you try to invoke show() of class
B, the compiler will get confused as to which show() to be invoked ( either
from class B or class C ). Hence it leads to ambiguity.
For Example:
class Demo1
{
//code here
}
class Demo2
{
//code here
}
class Demo3 extends Demo1, Demo2
{
//code here
}
class Launch
{
public static void main(String args[])
{
//code here
}
}
In the above code, Demo3 is a child class which is trying to inherit two par-
ent classes Demo1 and Demo2. This is not permitted as it results in a dia-
mond problem and leads to ambiguity.
NOTE: Multiple inheritance is not supported in Java but you can still
achieve it using interfaces.
It is a type of inheritance in which a class extends itself and form a loop it-
self. Now think if a class extends itself or in any way, if it forms cycle within
the user-defined classes, then is there any chance of extending the Object
class. That is the reason it is not permitted in Java.
For Example:
class Demo1 extends Demo2
{
//code here
}
class Demo2 extends Demo1
{
//code here
}
In the above code, both the classes are trying to inherit each other’s charac-
ters which is not permitted as it leads to ambiguity.
For Example:
class You
{
private int an;
private int pw;
You{
an =111;
pw= 222;
}
}
class Friend extends You
{
void change Data()
{
an =8888;
pw=9999;
}
}
void disp()
{
System.out.println(an);
System.out.println(pw);
}
}
class Launch
{
public static void main(String args[])
{
Friend f = new Friend();
f.change.Data();
f.disp();
}
}
When you execute the above code, guess what happens, do you think the
private variables an and pw will be inherited? Absolutely not. It remains the
same because they are specific to the particular class.
class B extends A{
B();}
class B extends A{
B() { super(); }
}
Imagine you add getEmployeeDetails to the class Parent as shown in the be-
low code:
public String getEmployeeDetails() {
return "Name: " + name;
}
Here we have used a Child class as a Parent class reference. It had a special-
ized behavior which is unique to the Child class, but if we invoke getEmploy-
eeDetails(), we can ignore the functionality difference and focus on how Par-
ent and Child classes are similar.
As you already know, constructors do not get inherited, but it gets executed
because of the super() keyword. ‘super()’ is used to refer the extended class.
By default, it will refer to the Object class. The constructor in Object does
nothing. If a constructor does not explicitly invoke a super-class construc-
tor, then Java compiler will insert a call to the no-argument constructor of
the super-class by default.
Conclusion :-
Reference :-
www.sites.google.com
https://www.javatpoint.com/inheritance-in-java
https://beginnersbook.com/2013/05/java-multiple-inheritance/