Inheritance
In nautural environment it is common to find objects inheriting from specific types. a shape can be any thing a rectangle ,circle,squar polygon etc. I.e. they are all a type of shape. Similarly classes can inherit charecters of other classes, this is done by using sxtends keyword. You can access the constructor of parent class by using the super() function from the child class constructor,where it should be specified as the first statement.
Example
package com.vivarem.examples;
class Shape{//base class
public void printMsg(String shape){System.out.println("I am "+shape);}
public double area(){return 0;}
}
class Rectangle extends Shape{//child class of shape
int length,breadth;
Rectangle(int l,int b){
length=l;breadth=b;
}
public double area(){
return length*breadth;
}
public void show(){
printMsg("Rectangle with area"+area());
}
}
class Circle extends Shape{//child class of shape
int radius;
Circle(int r){
radius=r;
}
public double area(){return 3.14*radius*radius;}
public void show(){
printMsg("Circle with area"+area());
}
}
public class JavaInheritance{
public static void main(String[] a){
Rectangle r=new Rectangle(10,20);
r.show();
Circle c= new Circle(10);
c.show();
System.out.println();
}
}
In the above example Shape is the base class with 2 methods printMsg and area. The classes Rectangle and Circle inherits the properrties of Shape, here the methods area and PrintMsg methods. Both classes Rectangle and Circle overides the area method of Shape..Overriding is way of having a local method of the same method signature of the parent/base class. so when we call the method method invoked will be of the child class rather than the parent.overdiing is employed when child properties differ from parent properties.
Polymorphism
Polymorphism is an OOP concept whereby a single object exhibits different nature when invoked differently.Ploymorphism can be explained in detail with following example. Circle ,Rectangle are examples from previous example
public class JavaPolymorphism{
public static void main(String[] a){
Rectangle r=new Rectangle(10,20);
r.show();
Circle c= new Circle(10);
c.show();
Shape s=r;
System.out.println("rectangle"+s.area());
s=c;
System.out.println("circle"+s.area());
}
}
Here same object s of class Shape displays the output of Rectangle area and Circle area dynamically.
final Keyword and inheritance
if a class has been specified final as
final class JavaFinal{
}
then it can't be extended by other classes, in other words you will have a final parent/base class.
Access control and Inheritance
public data members and methods are aviablable from all classes and methods
public methods and data members can be inherited to any class.protected data memebers and methods can be inherited to all sub classes. default data members and methods are inherited to all inheriting classes of same package and not any child class outside the package.The minimum access specifier required by a class for it being extendable outside the current package is public. private data memebers and functions are not inheritable at all.
< Arrays |
Content |
Inner Classes > |