Inheritance in Java (OOP Concept 2)



Object Oriented Programming...There are some key concepts built on OOP. In my previous article I explained about Encapsulation. This is the second one! This time I will discuss about Inheritance...I think you can get the idea from the word itself. Got it?
Then what is Inheritance

Inheritance

This means creating new classes using the existing classes and including the extended attributes and methods of that existing class. Then the existing class become the Super class or Parent. The newly created class is called Sub class or Child. This sub classes always have the functionalities and properties that the super class have. Apart from them there can be unique functionalities and properties to the sub class. As soon as a sub class is created , a relationship is built. Let's see.

Super class: Animal
Sub class: Dog
Relationship:  Dog is an animal

SYNATX:

// Super Class
class Animal{

}
// Sub Class
class Dog extends Animal{

}

KEYWORD for creating sub classes : extends

KEY FACTS: 
Whenever a sub class is created, all the methods and attributes in its super class, are inherited to the sub class.

class Animal{
int age = 5;
void eat(){
System.out.println("Animal eats");
}
void sleep(){
System.out.println("Animal sleeps");
}
}
class Dog extends Animal{
void bark(){
System.out.println("Dog barks");
}
}

class Demo{
public static void main(String args[]){
Dog dog = new Dog();
// call methods from super class
dog.sleep();
dog.eat();
// call method from sub class
dog.bark();
// get attribute value from super class
System.out.println("Age : " + dog.age);

}
}

Output:


A sub class reference variable created within the sub class scope, can use the super class attributes and methods.

class Animal{
// age is not initialized
int age;
void eat(){
System.out.println("Animal eats");
}
void sleep(){
System.out.println("Animal sleeps");
}
}
class Dog extends Animal{
void bark(){
System.out.println("Dog barks");
}
}

class Demo{
public static void main(String args[]){
Dog dog = new Dog();
// use age attribute by reference variable dog
dog.age = 5;
// get attribute value from super class
System.out.println("Age : " + dog.age);

}
}

Output:

When a sub class is inherited from a super class, the codes are not copied to each other. The only thing happening is linking the two classes.

When an object is created from a sub class, all the constructors of the super classes above in the class hierarchy must be loaded. Reason: All the attributes that are inherited to the sub class from super class, are defined within the constructors of those super classes.

super() keyword:
This decides the constructor to be loaded from the super class..When super() statement is placed within the sub class constructors, it invokes the relevant constructor from super class. Otherwise, Java compiler inserts the default constructor always to the sub class.

class A{
int a;
A(){
System.out.println("A()");
}
A(int i){
System.out.println("A(int i)");
}
A(int i, int j){
System.out.println("A(int i, int j)");
}
}
class B extends A{
int b;
B(){
super();
System.out.println("B()");
}
B(int i){
super(i);
System.out.println("B(int i)");
}
B(int i, int j){
super(i, j);
System.out.println("B(int i, int j)");
}
}
class Demo{
public static void main(String args[]){
new B();
System.out.println("-------------");
new B(100);
System.out.println("-------------");
new B(100,200);
}
}

Output:

These are the important things that I needed to tell you. I think now you have some knowledge to work with Inheritance in Java.

It's time to wrap up the session. If you have any question, please drop a comment.

Good Luck!



Admin Semoga Informasi Yang Diberikan Melalui Web Ini Dapat Bermanfaat Bagi Saudara/i

No comments for "Inheritance in Java (OOP Concept 2)"