Header Ads

Hii,I am Anand Nayak.This blogger is make for specially basic programs of some language.

Inheritance

 Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.

The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).

extends Keyword

extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword.

Syntax

class Super {
   .....
   .....
}
class Sub extends Super {
   .....
   .....
}

Sample Code:

class a {
protected void f1() {
System.out.println("Base Class");
}
}
class b extends a{

public void f1() {
System.out.println("Derived Class");
}
}

public class base {
public static void main(String [] args)
{
a obj =new b();
obj.f1();
}
}


Output:   Derived Class

3 comments: