Inheritance

 package com.company;

class Animal {
private String name;
private String colour;
private String sound;
public String getColour() {
return colour;
}
public void setColour(String colour) {
this.colour = colour;
}
public String getSound() {
return sound;
}
public void setSound(String sound) {
this.sound = sound;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}


}
class Dog extends Animal{
String breed;
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
}
class Base{
int x;

public int getX() {
return x;
}
public void setX(int x) {
System.out.println("I am in Base and setting x");
this.x = x;
}
public void printMe(){
System.out.println("I am constructor");
}
}
class derived extends Base{
int y;

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}
}
public class CWH_45_inheritance {
public static void main(String[] args) {
// Creating object of Base class
//Base rohan= new Base();
//rohan.setX(4);
// System.out.println(rohan.getX());
// Creating object of derived class
// derived roh=new derived();
// roh.setX(67);
// System.out.println(roh.getX());
// Quiz
Animal a= new Animal();
Dog d=new Dog();
d.setColour("Black");
d.setSound("Bark");
d.setName("Chunnu");
d.setBreed("German sufferd");
System.out.println(d.getColour());
System.out.println(d.getSound());
System.out.println(d.getName());
System.out.println(d.getBreed());
}
}

Comments