0% found this document useful (0 votes)
13 views8 pages

Inheritance

Uploaded by

tusharsensahoo56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views8 pages

Inheritance

Uploaded by

tusharsensahoo56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Encapsulation in java

Encapsulation in Java is a process of wrapping code and data together into a


single unit

We can create a fully encapsulated class in Java by making all the data members of
the class private. Now we can use setter and getter methods to set and get the data
in it.

Example:

Package pack; //syntax to create user defined package

public class Student


{
private String name; //private data member
public String getName() //getter method for name
{
return name;
}
public void setName(String name) //setter method for name

{
this.name=name
}
}
import pack;
class Test
{
public static void main(String[] args)
{
Student s=new Student(); //creating instance of the encapsulated class
s.setName("vijay"); //setting value in the name member
System.out.println(s.getName()); //getting value of the name member
}
}
Advantage of Encapsulation in Java

By providing only a setter or getter method, you can make the class read-only or
write-only. Or we can skip the getter or setter methods.

1. It provides you the control over the data. Suppose you want to set the
value of id which should be greater than 100 only, you can write the logic
inside the setter method. You can write the logic not to store the negative
numbers in the setter methods.
2. It is a way to achieve data hiding in Java because other class will not be able
to access the data through the private data members.
3. The encapsulate class is easy to test. So, it is better for unit testing.
4. The standard IDE's are providing the facility to generate the getters and
setters. So, it is easy and fast to create an encapsulated class in Java.

Inheritance in java
 A mechanism in which one object acquires all the properties and behaviours
of a parent object or inherit properties of a parent object.
 The idea behind inheritance is we can create new classes that are built upon
existing classes.
 Inheritance represents the IS-A relationship which is also known as
a parent-child relationship
o It reuses methods and fields of the parent class Moreover; you can add
new methods and fields in your current class also.
 Class Inheritance in Java is used to build new Classes from existing Classes.
 The inheritance relationship is also transitive (passing over to)
 For example, a car Class can inherit some properties from vehicle Class.
 Here the base Class is the vehicle Class and the subclass is more specific car
let i20 Class.
 A subclass must use the extends clause to derive from a super class
 The subclass inherits members of the super class and hence promotes code
reuse.
 The subclass (child class) itself can add its new behaviour and properties.
 For example the java.lang.Object Class is always at the top of any Class
inheritance hierarchy.
 extends keyword is used to inherit properties from parent class to child class
 parent class is also called super class or base class and the child class is also
called subclass or derived class

Why use inheritance in java

o For Method Overriding (so runtime polymorphism can be achieved).


o For Code Reusability.

Terms used in Inheritance

o Class: A class is a group of objects which have common properties. It is a


template or blueprint from which objects are created.

o Sub Class/Child Class: Subclass is a class which inherits the other class. It is
also called a derived class, extended class, or child class.

o Super Class/Parent Class: Superclass is the class from where a subclass


inherits the features. It is also called a base class or a parent class.

o Reusability: reusability is a mechanism which facilitates you to reuse the


fields and methods of the existing class when you create a new class. We can
use the same fields and methods already defined in the previous class.

syntax
class Subclass-name(child) extends Superclass-name(parent)
{
//methods and fields
}
 extends keyword indicates that you are making a new class that derives
from an existing class.
 The meaning of "extends" is to increase the functionality.
 In the terminology of Java, a class which is inherited is called a parent or
super class, and the new class is called child or sub class

Example:

 Here Programmer is the subclass and Employee is the superclass.


 The relationship between the two classes is Programmer IS-An Employee.
 It means that Programmer is a type of Employee.

class Employee
{
float salary=40000;
int id=90;
}
class Programmer extends Employee
{
int bonus=10000;
public static void main(String args[])
{
Programmer p=new Programmer();
System.out.println("id of Programmer is:"+p.id);
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Types of inheritance in java
On the basis of class, there can be three types of inheritance in java:
 single,
 multilevel and
 hierarchical.

 Note: multiple and hybrid inheritance is supported through interface only.

Note: multiple and hybrid inheritance are not supported in Java through class but
supported through interface only.
Single Inheritance
 When a class extends another one class only then we called it a single
inheritance
 Here in the below example parentA is a parent class of childB and childB
would be a child class of parentA.

Example:
Class A
{
public void methodA()
{
System.out.println("Base class method");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}
}

Multilevel Inheritance
 When there is a chain of inheritance, it is known as multilevel inheritance.
 Here in the below class C (is grandchild class of class A)inherits property from
B, B inherits from A where A is a parent class and hence construct a
multilevel inheritance.

Class A
{
public void methodA()
{
System.out.println("Class A method");
}
}
Class B extends A
{
public void methodB()
{
System.out.println("class B method");
}
}
Class C extends B
{
public void methodC()
{
System.out.println("class C method");
}
public static void main(String args[])
{
C obj = new C();
obj.methodA(); //calling grand parent class method
obj.methodB(); //calling parent class method
obj.methodC(); //calling local method
}
}
Hierarchical Inheritance
When two or more classes inherit a single class, it is known as hierarchical
inheritance. In the example given below, the Dog and Cat classes inherit the Animal
class, so there is in the Example:

class A
{
public void methodA()
{
System.out.println("method of Class A");
}
}
class B extends A
{
public void methodB()
{
System.out.println("method of Class B");
}
}
class C extends A
{
public void methodC()
{
System.out.println("method of Class C");
}
}
class D extends A
{
public void methodD()
{
System.out.println("method of Class D");
}
}
class hieracl_inhrt
{
public static void main(String args[])
{
B o1 = new B();
C o2 = new C();
D o3 = new D();
//All classes can access the method of class A
o1.methodA(); //o1.methodB()
o2.methodA(); //o2.methodC()
o3.methodA(); //
o1.methodB(); //class B method() similarly class C and D can
}
}

Multiple and Hybrid inheritance


 one class inherits features from multiple classes, it is known as multiple
inheritance.
 java programming, multiple and hybrid inheritances are supported through
the interface
 Hybrid inheritance is a combination of hierarchical and Multiple inheritance
y

Why multiple inheritance is not supported in Java?


 the concept of one class extending (Or inherits) more than one base class.
 The problem with “multiple inheritance” is that the derived class will have to
manage the dependency on more base classes.
 Multiple Inheritance is very rarely used in software projects. Using Multiple
inheritance often leads to problems in the hierarchy. This results in unwanted
complexity when further extending the class.
 To reduce the complexity and simplify the language, multiple inheritance is
not supported in Java.
 Consider a scenario where A, B, and C are three classes. The C class inherits A
and B classes. If A and B classes have the same method and you call it from
child class object, there will be ambiguity to call the method of A or B class.
 Since compile-time errors are better than runtime errors, Java renders
compile-time error if you inherit 2 classes. So whether you have same method
or different, there will be compile time error.

class A
{
void msg()
{
System.out.println("Hello");
}
}
class B
{
void msg()
{
System.out.println("Welcome");
}
}
class C extends A, B
{
//suppose if it were
public static void main(String args[])
{
C obj=new C();
obj.msg();//Now which msg() method would be invoked?
}
}

Uses of inheritance in java


o For Method Overriding (so runtime polymorphism can be achieved).
o For Code Reusability.
o Static Methods or variables do not take part in inheritance.
o Even though static methods or variables do not take part in inheritance and cannot
be overridden, they can be redefined in a subclass. The redefinition is not called
overridden but hidden.

Invalid concept in Java Class Inheritance


 Private members of the super class are not inherited by the subclass and can only be
indirectly accessed.
 Since constructors and initializer blocks are not members of a Class, they are not
inherited by a subclass.
 A subclass can extend only one superclass
 Members that have default accessibility in the superclass are also not inherited by
subclasses in other packages, as these members are only accessible by their simple
names in subclasses within the same package as the superclass.

You might also like