Friday, January 30, 2015

JAVA INHERITANCE

Java Inheritance

October 2014. About this time, I already bought an Asus computer from Fry’s because I was going to need it to study for the MCSA Configuring Windows 8.1 Exam 687. I bought it for less than a public junior college semester course.

As you might know already, unless you don’t read technical web pages, Windows 8.1 is going to be off the market soon and so will its derivate Windows 9. Microsoft is churning out Windows 10 instead.

Windows 8 could not compete with its predecessor Windows 7's likeability. Among the flaws of Windows 8 I have encountered is that the tile environment is separated from the desktop environment. For example, if you log in to a WiFi in the desktop environment, you need to log in again in the tile environment. However, it seems that this flaw might have been corrected by a Windows 8 update.

Currently, I am taking an Advanced Java class and we are studying Java inheritance. Java allows only single inheritance, but you can simulate multiple inheritance using interfaces.

Here’s a Java program that shows constructor chaining in an inheritance.

public class Bird extends Pterodactyl {
  public String toString() {
    return "I am a bird.";
  }
  public static void main(String[] args) {
    Bird bird = new Bird();
    System.out.println(bird.toString());
  }
}

class LivingThings {
  public LivingThings() {
    System.out.print("I am a living thing. ");
  }
}

class Animals extends LivingThings {
  public Animals() {
    System.out.print("I am an animal. ");
  }
}

class Dinosaurs extends Animals {
  public Dinosaurs() {
    System.out.print("I am a dinosaur. ");
  }
}

class Pterodactyl extends Dinosaurs {
  public Pterodactyl() {
    System.out.print("I am a pterodactyl. ");
  }
}

Here is the console run and output of the above program.
> run Bird
I am a living thing. I am an animal. I am a dinosaur. I am a pterodactyl. I am a bird.
>

CYK:
What keyword is use to create instances in Java?
What keyword is use to call the superclass constructor or superclass method?
What’s the new paradigm that is replacing the swing package GUI capability?

Look up in the sky! It's a bird! ...

John Sindayen

Reference:
http://java67.blogspot.com/2012/12/how-constructor-chaining-works-in-java.html

1 comment:

  1. Inheritance is a important concept in object oriented programming and I learnt it in Online Java Course

    Java Online Training

    ReplyDelete