0% found this document useful (0 votes)
15 views3 pages

Code 170967

The document contains Java code for a parent class 'Dog' with attributes, a constructor, and methods for running and barking. It also includes a child class 'GermanShepard' that extends 'Dog', adding its own attributes and overriding the run and bark methods to provide specific behavior. This demonstrates basic inheritance and method overriding in object-oriented programming.

Uploaded by

irshadrayn2414
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Code 170967

The document contains Java code for a parent class 'Dog' with attributes, a constructor, and methods for running and barking. It also includes a child class 'GermanShepard' that extends 'Dog', adding its own attributes and overriding the run and bark methods to provide specific behavior. This demonstrates basic inheritance and method overriding in object-oriented programming.

Uploaded by

irshadrayn2414
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Class Dog Code: (Parent Class)

public class Dog


{
//attributes
private String breed;
private int age;
private String color;
//Constructor
public Dog(String breed, int age, String color)
{
this.breed = breed;
this.age = age;
this.color = color;
}
//accessor method
public String getBreed()
{
return breed;
}
//mutator method
public void setBreed(String breed)
{
this.breed = breed;
}
//methods
public void run()
{
System.out.println("Every dog can run.");
}
public void bark()
{
System.out.println("Every dog can bark.");
}
}

Class GermanShepard Code: (Child Class)


public class GermanShepard extends Dog
{
//attributes
private String petName;
private double price;
//Constructor
public GermanShepard(String breed, int age, String color, String
petName, double price)
{
super(breed, age, color);
this.petName = petName;
this.price = price;
}
//method override
@Override
public void run()
{
super.run();
System.out.println("German Shepard runs faster.");
System.out.println(getBreed());
}
@Override
public void bark()
{
super.bark();
System.out.println("German Shepard bark louder.");
}
}

You might also like