-3

Is it possible to inherent some of the property from the parent class to child class in java.

In one of my situation i have to take only 3 properties from the parent while my parent has 100 property.

If yes then what is the way.?

Thanks in advance.

Danish
  • 189
  • 2
  • 3
  • 14
  • Yes, obviously u can inherit parent class properties. Tell more about what issue you are facing to achieve that. – Shafin Mahmud Dec 04 '19 at 09:47
  • Does this answer your question? [Do subclasses inherit private fields?](https://stackoverflow.com/questions/4716040/do-subclasses-inherit-private-fields) – Turamarth Dec 04 '19 at 09:47
  • *while my parent has 100 property* sounds like you have tto think about your object structure – Jens Dec 04 '19 at 09:48

2 Answers2

1

I recommend you either refactor your code, or simply don't use the 97 properties you don't need.

Your super-class shouldn't be more complex than your subclass. The purpose of your subclass should be to implement or add functionality. Inserting a lot of properties that you won't use defeats the point of inheritance and class hierarchies.

Perhaps you can divide the many properties of your super-class and instead put them in sub-classes.

nylanderDev
  • 531
  • 4
  • 14
0

Yes, We can do it. Just Extend the parent class

 class Teacher {
       String designation = "Teacher";
       String collegeName = "Beginnersbook";
       void does(){
        System.out.println("Teaching");
       }
    }

public class PhysicsTeacher extends Teacher{
   String mainSubject = "Physics";
   public static void main(String args[]){
    PhysicsTeacher obj = new PhysicsTeacher();
    System.out.println(obj.collegeName);
    System.out.println(obj.designation);
    System.out.println(obj.mainSubject);
    obj.does();
   }
}

Java Inheritance you can check out this link to learn more.

Tejas Dhawale
  • 393
  • 1
  • 9