What, Why, and How to code inheritance? How can you access private instance data in a super class from a subclass? What is purpose to override toString() method? Use of coding examples to explain your answer.
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
// The language is java
What, Why, and How to code inheritance? How can you access private instance data in a super class from a subclass? What is purpose to override toString() method? Use of coding examples to explain your answer.
Inheritance is a feature of an Object Oriented Programming where the methods and members of a parent class can be used by its child class.
Programmatically a child class can access the methods and members of parent class by using "extends" keyword.
The private members cannot be accessed directly outside the program. So, we cannot use instance variables directly and thus we created getters and setters for instance variables and with these methods we can access them in subclass.
toString() is a method in Object class in lang package which is used to customize the output when we tried to print the object.
When we try to print an object the reference address of an object is printed. To avoid this we print a customize output.
Step by step
Solved in 5 steps with 1 images