Вы находитесь на странице: 1из 3

Concept of Inheritance

Inheritance:
It allows the creation of hierarchical classifications. Using Inheritance you can extend
properties of your base class. To inherit a class, you simply incorporate the definition of
one class into another by using the extends keyword. Base class known as Super class and
child class known as Sub class.

Example:- class B extends A { }

There are some types of Inheritance:-
1. Single Inheritance (one to one) // Base class



// Child class

2. Multiple Inheritance(many to one)

// Base class


// Child Class

3. Hierarchical Inheritance(one to many)
// Base class



// Child Class

4. Multilevel Inheritance(one to next)






5. Hybrid Inheritance(many to many)

Combination of all above.



Access Specifiers:
private: Data member(variable) can use in same class only.
protected: Data member(variable) can use in same class , sub class and in non subclass in
same package.
private protected: Data member(variable) can use in same class, sub class and in other
package's subclass.
default: Data member(variable) can use in same class, sub class and in other package's
subclass.
public: Data member(variable) can use in all class in same package and in other packages.

Chapter-7
Class-12
th

A
B
C
A B
A
B C D
A
B
C

Function Overloading:-
Whenever a same signature (name) method called more than one time within same class,
with different number or types of arguments, its called Function overloading.
Definition:- A function name having several definition in the in same scope that are
differentiable by the number or type of their arguments, is said to be an overloading
function.

Class A
Number of arguments
Set() Set(int x) Set(int x, int y) Set(int x, int y, int z)
type of arguments
Set(int x) Set(float x) Set(double x) Set(String x)

Function Overriding:-
Whenever a same signature (name) method called more than one time within super and
sub class with same number or types of arguments, its called Function overriding.
Definition:- A method in a subclass hides or overshadows a methods inherited from the
super class if both methods have same name signature, its known as overriding the
inherited methods.

Class A Class B extends A Class C extends B
Number of arguments
Set(int x) Set(float x) Set(String x)
type of arguments
Set(int x) Set(int x) Set(int x)

Constructor:-
A constructor initializes an object immediately upon creation. It has the same name as
the class in which it resides and is syntactically similar to a method. Once defined, the
constructor is automatically called immediately after the object is created, before the
new operator completes. Constructors look a little strange because they have no return
type, not even void. It is the constructors job to initialize the internal state of an object
so that the code creating an instance will have a fully initialized, usable object
immediately.
class A
{
A()
{
}
}


Abstract Classess:
An Abstract class is the one that simply represent a concept and whose objects can't be
created. it is created through the use of keyword abstract.
abstract class A
{

}
Rules for Abstract class:
We can't create its object.
It should have at least one abstract method in it.
Its abstract method should have the definition in sub class.

Abstract methods: These also created with abstract keyword.
public abstract void area();


Interface:
Using interface, you can specify what a class must do, but not how it does it. Interfaces
are syntactically similar to classes, but they lack instance variables, and their methods are
declared without any body. It contains abstract methods and final variables.
Definition:- An Interface defines a protocol of behavior. The aim of interface in java is to
dictate common behavior among object from diverse classes.
interface i
{
}
class A implements i
{
}

It is not necessary to define all methods in the class. We can leave some methods without
definition.

Вам также может понравиться