You are on page 1of 4

Object -->It is a predefined class and it is acting as parent

for
all the remaining predefined or userdefined classes

clone(), eqals(),toString().......

Object Oriented Programming (OOP) Concepts


-------------------------------------------
1) Inheritance

2) Polymorphism

3) Abstraction

4) Encapsulation

Inheritance : --> It is a mechanism of inheriting parent class data into child


class.
-------------

The idea behind inheritance in Java is that you can create new classes that are
built upon existing classes.

When you inherit from an existing class, you can reuse methods and fields of the
parent class.

Moreover, you can add new methods and fields in your child class also.

Advantages of Inheritance:
-------------------------
1)code reusability

2)Method overriding

Parent Class or superclass:


--------------------------
Superclass is the class from where a subclass inherits the features.

It is also called a base class or a parent class.

Sub Class/Child Class:


----------------------
Subclass is a class which inherits the other class data.

It is also called a derived class, extended class, or child class.

syntax
------
class Subclass-name extends Superclass-name
{
code
}

-->extends keyword indicates that you are making a new class that derives from an
existing class.
-------

Types of inheritance:
--------------------

1)single inheritance

2)Hierarchial inheritance

3)Multilevel inheritance

4)Hybrid inheritance

5)Multiple Inheritance--->Not supported Directly

single inheritance: --->A class which extends exactly single class.


------------------

syntax
------
class A----------->parent class(A)
{
code--->properties,methods
}

class B extends A ----> child class(B)


{
class B contains code of class A + Additional code in class B
}

Hierarchial inheritance: -----> same parent class can extend by many child classes
-----------------------

syntax
------
class A----------->parent class(A)
{
code
}

class B extends A ----> child class(B)


{
class B contains code of class A + Additional code in class B
}

class C extends A ----> child class(c)


{
class C contains code of class A + Additional code in class c
}

Multilevel inheritance:
----------------------
Deriving a new class from another derived class.

There must be an intermediate class which acts as both parent and child.

syntax:
------

class A----------->parent class(A)


{
code
}

class B extends A ----> child class(B)-----> Here class B acts as parent for
class C.
{ In the same way it acts as child
to class A .
So class B is called as
intermediate class.

class B contains code of class A + Additional code in class B


}

class C extends B ----> child class(c)


{
class C contains code of class A + code of class B + Additional code in class
c
}

Hybrid inheritance: If two or more types of inheritance is combined in single


program then it is called hybrid inheritance.
------------------

In the below syntax we have combination of single inheritance + multilevel


inheritance

syntax:
-------

class A----------->parent class(A)


{
code
}

class B extends A ----> child class(B)


{
class B contains code of class A + Additional code in class B
}
class C extends B ----> child class(c)
{
class C contains code of class A + code of class B + Additional code in class
c
}

Multiple Inheritance:
--------------------

It is a process of inheriting more than one parent class at a time.

Java does not supports this type of inheritance. Because ambiguity occurs.

So to achieve this we will go for interface concept.

syntax:
-------

class A
{
public void m1()
{
System.out.println("Helloworld");
}
}

class B
{

public void m1()


{
System.out.println("Welcome");
}
}

class C extends A,B ----> child class(c)


{

You might also like