DEV Community

Neelakandan R
Neelakandan R

Posted on • Edited on

3 1 1 1 1

Task-7

Assignment 7:

Goal: Learning Inheritance, super keyword and Method Overriding
Grandma:

  1. Create a class called 'Grandma'.
  2. Declare Field as below in it.
    1. String name = "Stella";
  3. Have below methods in it.
    1. public void work()
  4. Have print statements as you wish in the above methods' definition.

Mother:

  1. Create a class called 'Mother' as Child class of Grandma.
  2. Declare Field as below in it.
    1. String name = "Arasi";
  3. Have below method in it.
    1. public void work()
  4. Have print statement to print 'name' and super.name in work() definition.
  5. Add 'super.work();' inside work() method.

Kid:

  1. Create a class called 'Kid' as Sub class of Mother.
  2. Declare Field as below in it.
    1. String name = "Suman";
  3. Define main method as 'public static void main(String[] args)
  4. Inside main method, create an instance called 'kid' for Kid class.
  5. Have below methods in it.
    1. public void work()
    2. public void study()
  6. Have print statements as you wish in the above two methods' definition.
  7. Call study() method from main method.
  8. Inside study() method, call work() method.
  9. Print name and super.name inside work() method.
package B15;

public class Grandma {

    String name = "stella";
    int age=80;
    public void work() {
        System.out.println("grandma has work");
    }

}
Enter fullscreen mode Exit fullscreen mode
package B15;

public class Mother extends Grandma {
    String name = "arasi";
    int age = 50;
    public void work() {
        System.out.println("Mother has work");
        System.out.println("Mother name = " + name);
        System.out.println("Grandma name = " + super.name);
        System.out.println("Grandma age = " +super.age);
        super.work();

    }
}
Enter fullscreen mode Exit fullscreen mode
package B15;

public class Kid extends Mother {

    String name= "pavi";
    int  age = 30;
    public static void main(String[] args) {
        Kid kid = new Kid();

        kid.work();
        kid.study();
    }

    public void work() {
        System.out.println("work of kid");
    }

    public void study() {
        super.work();
         System.out.println("Mother age = "+super.age);
         System.out.println("kid name = "+ name);
         System.out.println("kid age = "+ age);
         System.out.println("study of kid");
    }

}

Enter fullscreen mode Exit fullscreen mode

Output:

work of kid
Mother has work
name = arasi
Grandma name = stella
Grandma age = 80
grandma has work
Mother age = 50
kid name = pavi
kid age = 30
study of kid

Example for constructor invoke parent class constructor using keyword super

package B15;

public class Supermarket {

    public String productName;
    public int price;
    public int discount;
    public String name, place;

    Supermarket(String productName, int price, int discount) {
        this.productName = productName;
        this.price = price;
        this.discount = discount;

    }

    Supermarket(String name, String place) {
        this("biscuits", 30, 5);
        this.name = name;
        this.place = place;
    }

    public static void main(String[] args) {
        Supermarket product1 = new Supermarket("cinthol", 22, 2);
        Supermarket product2 = new Supermarket("deen supermarket", "velachery");
        product1.sell();
        product2.sell2();
    }

    private void sell2() {
        System.out.println(productName);
        System.out.println(price);
        System.out.println(discount);
        System.out.println(name + "\n" + place);

    }

    public void sell() {
        System.out.println(productName);
        System.out.println(price);
        System.out.println(discount);
    }
}

Enter fullscreen mode Exit fullscreen mode

Output:
cinthol
22
2
biscuits
30
5
deen supermarket
velachery

package B15;

public class Super extends Supermarket {

    Super(String productName, int price, int discount) {

        super(productName, price, discount);
    }

    public static void main(String[] args) {
        Super su = new Super("c1", 40, 2);
        su.display();
    }

    public void display() {
        System.out.println("productName = "+productName+"\n"+"price"+price+"\n"+"discount"+discount);

    }
}


Enter fullscreen mode Exit fullscreen mode

output:
productName = c1
price40
discount2

Retry later

Top comments (0)

Retry later
Retry later