Question

In: Computer Science

Write a short code segment which includes/illustrates the concepts of inheritance, overloading, and overriding in java language.

Write a short code segment which includes/illustrates the concepts of inheritance, overloading, and overriding in java language.

Solutions

Expert Solution

Inheritance is the concept of OOPs that one class can inherit the properties of another class. The main class is called parent class and the class which inherits this parent class is called child class. The keyword extends is used to inherit another class.

Human.java

public class Human{

private String name;

private int age;

public Human(String name, int age) {

super();

this.name = name;

this.age = age;

}

@Override

public String toString() {

return "Human [name=" + name + ", age=" + age + "]";

}

}

Men.java

public class Men extends Human{

private String gender;

public Men(String name, int age, String gender) {

super(name, age);

this.gender = gender;

}

@Override

public String toString() {

return "Men [gender=" + gender + ", toString()=" + super.toString() + "]";

}

}

Test.java

public class Test {

public static void main (String [] args) {

Men m = new Men("John", 22, "Male");

System.out.println(m);

}

}

Overridding:

Overriding is the concept of declaring the method in the child class with the same name and same no of parameters of the parent class. The logic in the child class method will be different to the one present in the parent class

Human.java

public class Human{

private String name;

private int age;

public Human(String name, int age) {

super();

this.name = name;

this.age = age;

}

public int getAge() {

return age;

}

@Override

public String toString() {

return "Human [name=" + name + ", age=" + age + "]";

}

}

Men.java

public class Men extends Human{

private String gender;

public Men(String name, int age, String gender) {

super(name, age);

this.gender = gender;

}

public int getAge() {

return super.getAge() + 1;

}

@Override

public String toString() {

return "Men [gender=" + gender + ", toString()=" + super.toString() + "]";

}

}

Test.java

public class Test {

public static void main (String [] args) {

Men m = new Men("John", 22, "Male");

System.out.println(m);

System.out.println("Call to the child class method: " + m.getAge());

}

}

Overloading:

Overloading is the concept of declaring a method with the same name multiple times in a class but with different number of parameters.

Calculation.java

public class Calculation{

public int add(int a, int b) {

return a + b;

}

public double add(double a, double b) {

return a + b;

}

}

Test.java

public class Test {

public static void main (String [] args) {

Calculation cal = new Calculation();

System.out.println(cal.add(8, 9));

System.out.println(cal.add(4.5, 6.8));

}

}


Related Solutions

Write a simple short Java program of your choice which illustrates inheritance, polymorphism and the use...
Write a simple short Java program of your choice which illustrates inheritance, polymorphism and the use of interfaces. The program should not take up more than half a page of size A4.
language is java Use method overloading to code an operation class called CircularComputing in which there...
language is java Use method overloading to code an operation class called CircularComputing in which there are 3 overloaded methods as follows: computeObject(double radius)-compute area of a circle computeObject(double radius, double height)-compute area of a cylinder computeObject(double radiusOutside, double radiusInside, double height)-compute volume of a cylindrical object These overloaded methods must have a return of computing result in each Then override toString() method so it will return the object name, the field data, and computing result Code a driver class...
// the language is java, please implement the JOptionPane Use method overloading to code an operation...
// the language is java, please implement the JOptionPane Use method overloading to code an operation class called CircularComputing in which there are 3 overloaded methods and an output method as follows: • computeObject(double radius) – compute the area of a circle • computeObject(double radius, double height) – compute area of a cylinder • computeObject(double radiusOutside, double radiusInside, double height) – compute the volume of a cylindrical object • output() use of JOptionPane to display instance field(s) and the result...
Lesson is on Inheritance and overriding the ToString ( ) method in Java: The program should...
Lesson is on Inheritance and overriding the ToString ( ) method in Java: The program should consist of 3 classes and will calculate an hourly paycheck.    - The superclass will contain fields for common user data for an employee (first name and last name) and a toString method that returns the formatted output of the names to the calling method.    - The subclass will calculate a paycheck for an hourly employee and will inherit from the superclass.   ...
Which diagram illustrates multiple inheritance?
Which diagram illustrates multiple inheritance? 
Which Feature of OOP illustrates code reusability? Abstraction, Encapsulation, Inheritance, Polymorphism or All of them.
Which Feature of OOP illustrates code reusability? Abstraction, Encapsulation, Inheritance, Polymorphism or All of them.
Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create...
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create an application with 5 classes: 1.) Vehicle 2.) Car 3.) Bus 4.) Truck 5.) Tester Following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassengers, isCpnvertable, numberSeats, maxWeightLoad, numberAxels **Class Vehicle: should have a constructor that initializes all of it's data **Classes Car, Bus, Truck: will have constructors that resuse their parents constructors and provide additional code for initalizing their specific data...
Language for this question is Java write the code for the given assignment Given an n...
Language for this question is Java write the code for the given assignment Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order.Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the matrix. Then the next line contains the n x n elements...
Using existing Stack Java Collection Framework, write Java Code segment to do the following.   You may...
Using existing Stack Java Collection Framework, write Java Code segment to do the following.   You may write this in jGrasp Create a Stack of String called, myStacks Read input from keyboard, 10 names and then add to myStacks As you remove each name out, you will print the name in uppercase along with a number of characters the name has in parenthesis. (one name per line).   e.g.     Kennedy (7) Using existing Stack Java Collection Framework, write Java Code segment to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT