Single Inheritance, Object Class

Java allows only single inheritance.

class TvDVD extends Tv, DVD {...}//error.

inherit deeper relation one and composite remains.

class TvDVD extends Tv {
    DVD dvd = new DVD();
    
    void play() {dvd.play();}
    void stop() {dvd.play();}
    void rew() {dvd.play();}
    void ff() {dvd.play();}
}
  • Object class : parent of every class

Class which has no parent inherits Object class automatically.

=> All of class inherit methods from Object class. ex) toString(), equals(Object obj), hashCode(),...

toString() : return class name and address(not exactly but meaningful)

//both are same result
System.out.println("c.toString");
System.out.println(c);

Last updated