Assignment 7:
Goal: Learning Inheritance, super keyword and Method Overriding
Grandma:
- Create a class called 'Grandma'.
- Declare Field as below in it.
- String name = "Stella";
- Have below methods in it.
- public void work()
- Have print statements as you wish in the above methods' definition.
Mother:
- Create a class called 'Mother' as Child class of Grandma.
- Declare Field as below in it.
- String name = "Arasi";
- Have below method in it.
- public void work()
- Have print statement to print 'name' and super.name in work() definition.
- Add 'super.work();' inside work() method.
Kid:
- Create a class called 'Kid' as Sub class of Mother.
- Declare Field as below in it.
- String name = "Suman";
- Define main method as 'public static void main(String[] args)
- Inside main method, create an instance called 'kid' for Kid class.
- Have below methods in it.
- public void work()
- public void study()
- Have print statements as you wish in the above two methods' definition.
- Call study() method from main method.
- Inside study() method, call work() method.
- 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");
}
}
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();
}
}
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");
}
}
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);
}
}
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);
}
}
output:
productName = c1
price40
discount2
Top comments (0)