// DESCRIPTION:
// Variable initialization and create display() on the super class
// Create subClass which extends superClass Person
// Assign value with the help of constructors
// Display the assigning values with the display() method as an output
// LEARN:
// Calling supper class constructor and its own constructor too
// Overriding Superclass Method and its own method too
// in sub class
public class MainActivity
{
public static void main(String[] args)
{
Dad d = new Dad("OM", 47, "male", "Pokhara", 50000, "Marketin");
d.display();
Mum m = new Mum("Uma", 44, "Female", "Butwal", 2000, "Main", "Sorry no desc");
m.display();
}
}
-------------------------------------------------------------
public class Person
{
String name;
int age;
String sex;
String city;
public void display()
{
System.out.println
(
name+"\n"+
age+"\n"+
sex+"\n"+
city
);
}
}
------------------------------------------------------------------------
// Overriding Superclass Method and its own method too
public class Dad extends Person
{
double salary ;
String job;
Dad(String name, int age , String sex, String city, double salary, String job)
{
this.name = name;
this.age = age;
this.sex = sex;
this.city = city;
this.salary = salary;
this.job = job;
}
@Override
public void display()
{
super.display();
System.out.println(salary);
System.out.println(job);
}
}
--------------------------------------------------------------------------
// Calling supper class constructor and its own constructor too
// Overriding Superclass Method and its own method too
public class Mum extends Dad
{
String desc;
public Mum(String name, int age , String sex, String city, double salary, String job,String desc)
{
super(name, age, sex, city, salary, job); // assigning super constructor
this.desc = desc;
}
@Override
public void display()
{
System.out.println();
super.display();
System.out.println(desc);
}
}
------------------------------------------------------------------------------------

No comments:
Post a Comment