Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How Inheritance works in Java through extends keyword ?

Program to demonstrate how Inheritance works in Java through extends keyword.

 class Shape {
    private String s = " Shape: ";
 
    public void draw(){
 name(" draw() ");
    }
    public void redraw(){
 name(" redraw() ");
 }
    public void area(){
 name(" area() ");
    }
    public void circumference(){
 name(" circumference() ");
    }
    public void name(String a){
 s+=a;
    }
    public String toString() {
 return s;
    }
 
    public static void main(String[] args) {
  
    Shape s = new Shape();
    s.draw();
    s.redraw();
    s.area();
    s.circumference();
    System.out.println(s);

    }

}

 
public class Circle extends Shape{

    public void area(){
 name(" Circle.area() ");
    }
    public void circumference(){
 name(" Circle.circumference() ");
    }
    
    public static void main(String[] args) {
  
 Circle c = new Circle();
    c.draw();
    c.redraw();
    c.area();
    c.circumference();

    System.out.println(c);
    Shape.main(args);


 }

}




Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot