简体   繁体   中英

Having problem with method calling in Inheritance

I used implementing inheritance in my program and the method in my subclass is not being called in the main method. It's showing the error "The method getArea() is not defined in type Second". the same problem with getPerimeter() method.

I've tried setting the values and changing parameters.

package firstproject;
import java.util.Scanner;
import java.util.Date;
import java.util.ArrayList;
public class Second{
  public String color="red" ;
  public boolean filled;
public Second() {

}
public Second(String tcolor, boolean tfilled) {
  tcolor=color;
  tfilled=filled;
}
public String getColor() {
  return color;
}
public boolean getfilled() {
   return filled;
}
public void setColor(String tcolor) {
   tcolor=color; 
}
public void setFilled(boolean tfilled) {
   tfilled=filled;
}
public String toString() {
   return "Color is =" +color+ " and it is filled or not = " 
   +filled;
 }
class myclass extends Second {
   double s1=1.0;
   double s2=1.0;
   double s3=1.0;
public myclass(){

 }
public myclass(double s4, double s5, double s6) {
s4=s1;
s5=s2;
s6=s3;
 }
double gets1() {
return s1;
}

double gets2() {
return s2;
}
double gets3() {
return s3;
}
public void sets1(double s4) {
s4=s1;
}
public void sets2(double s5) {
s5=s2;
}
public void sets3(double s6) {
s6=s3;
}
public double getArea() {
return (s2*s3)/2;
}
public double getPerimeter() {
return s1+s2+s3;
}
}
public static void main(String[] args) {
    System.out.println("Enter the three sides = ");
    Scanner input=new Scanner(System.in);
    int side1=input.nextInt();
    int side2=input.nextInt();
    int side3=input.nextInt();
    Second Triangle= new Second();
    System.out.println("Enter the color = ");
    String colo=input.next();
    System.out.println("The boolean value = ");
    String fil =input.next();
    System.out.println("The area of the triangle is = " 
  +Triangle.getArea());
    System.out.println("The perimeter of the triangle is = " 
  +Triangle.getPerimeter());
    System.out.println("The color in which it is filled is = " 
  +Triangle.getColor());
    System.out.println("If it is filled or not = " 
  +Triangle.getfilled());
}
}

It's showing the error "The method getArea() is not defined in type 
Second". Also, the same stuff is happening with the getPerimeter() 
method. So, I had the question of how to solve the code and is it an   
error related to subclass? The question is something like: 
(The Triangle class) Design a class named Triangle that extends
GeometricObject. The class contains:
■ Three double data fields named side1, side2, and side3 with 
default values
1.0 to denote three sides of the triangle.
■ A no-arg constructor that creates a default triangle.
■ A constructor that creates a triangle with the specified side1, 
side2, and
side3.
■ The accessor methods for all three data fields.
■ A method named getArea() that returns the area of this triangle.
■ A method named getPerimeter() that returns the perimeter of this 
triangle.
■ A method named toString() that returns a string description for 

the triangle. For the formula to compute the area of a triangle, see Programming Exercise 2.15. The toString() method is implemented as follows: return "Triangle: side1 = " + side1 + " side2 = " + side2 + " side3 = " + side3; Draw the UML diagrams for the classes Triangle and GeometricObject and implement the classes. Write a test program that prompts the user to enter three sides of the triangle, a color, and a Boolean value to indicate whether the triangle is filled. The program should create a Triangle object with these sides and set the color and filled properties using the input. The program should display the area, perimeter, color, and true or false to indicate whether it is filled or not.

In think you are getting confused between polymorphism and inheritance .

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).

The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.

Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

Polymorphism in Java is a concept by which we can perform a single action in different ways. Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms.

There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding.

If you overload a static method in Java, it is the example of compile time polymorphism. Here, we will focus on runtime polymorphism in java.

What you done here is Inheritance . So the parent class methods properties and method are inherited to child class and vice versa is not true . In your case Second is parent Class and myClass is a child class of Second class. As getArea method is defined in myClass , which is a child class, so parent class Second does not have details about getArea method. So you are getting this error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM