Java Inheritance Example Assign Hira Khan
Java Inheritance Example Assign Hira Khan
1:
// Parent class
class Animal {
void sound() {
System.out.println("Makes sound");
//Child class
void Dogsound() {
System.out.println("Barks");
void Catsound() {
System.out.println("Meows");
```
Example No.2
class Shape {
void draw() {
System.out.println("Drawing shape");
}
class Circle extends Shape {
void CircleDraw() {
System.out.println("Drawing circle");
void RectangleDraw() {
System.out.println("Drawing rectangle");
```
```
class Vehicle {
void move() {
System.out.println("Moving");
void Move() {
System.out.println("Driving");
void Move() {
System.out.println("Riding");
```
Example 4
class Employee {
String name;
int salary;
this.name = name;
this.salary = salary;
super(name, salary);
super(name, salary);
```
Example No.5
// Grandparent Example
class Grandparent {
void tradition() {
System.out.println("Following tradition");
void culture() {
System.out.println("Following culture");
//Example No6
void language() {
System.out.println("Speaking language");
//Example No.7
```
class Vehicle {
void move() {
System.out.println("Moving");
void move() {
System.out.println("Driving");
}
class Truck extends Vehicle {
void move() {
System.out.println("Hauling");
```
class Printable {
void print() {
System.out.println("Printing document");
void scan() {
System.out.println("Scanning");
//Example 9
class Person {
String name;
Person(String name) {
this.name = name;
int salary;
Person(name);
this.salary = salary;
//Example 10
class Person {
this.name = name;
this.age = age;
this.rollNumber = rollNumber;
this.course = course;
this.department = department;
this.designation = designation;
}
student.displayStudentInfo();
faculty.displayFacultyInfo();
```
*Output:*
```
Age: 20
Age: 35
Department: Computer Science
Designation: Professor