Java: Calling base class constructor

This entry is part 39 of 54 in the series Learn Java

Parameterized constructors have to be called manually by your program. This is also applicable to inheritance.

Suppose, a super class has a parameterized constructor, then it is the duty of the child class constructor to call the super class’s constructor first before executing any other Java statements inside its constructor, otherwise JVM will generate a compile time error.

Child class constructor will use super() method along with the required argument values to call super class constructor.

Finally, keep in mind that any other member functions apart from constructor cannot call super class’s constructor.

It is obvious that super class’s object will be constructed first before child class’s object construction.

package javaapplication29;

class Dog {

    String name;
    String colour;
    String diet;

    public Dog(String name1, String colour1, String diet1) {
        name = name1;
        colour = colour1;
        diet = diet1;
    }

    public Dog(String colour1) {
        colour = colour1;
    }

    public void eat(String food) {
        diet = food;
    }

    public void bark() {
        System.out.println("Dog " + name + " can bark: Lol Lol Lol");
    }

    public void showDog() {
        System.out.println("Dog name: " + name);
        System.out.println("Dog colour: " + colour);
        System.out.println("Dog can eat food: " + diet);
    }
    
    public static void main(String[] args) {
        PetDog petDog = new PetDog("Tommy", "white", "meat", "Rajkumar");
        petDog.showDog();
        petDog.showLove();

        StrayDog strayDog = new StrayDog("black");
        strayDog.showDog();
        strayDog.bite();
    }
}

class PetDog extends Dog {

    String owner;

    public PetDog(String name, String colour, String diet, String owner1) {
        // super() should be the first statement
        super(name, colour, diet);
        owner = owner1;
    }

    public void showLove() {
        System.out.println("Dog " + name + " can express love: Ich Ich Ich");
    }
}

class StrayDog extends Dog {

    public StrayDog(String colour) {
    // super() should be the first statement
        // stray dog has only colour, and call overloaded constructor
        super(colour);
    }

    public void bite() {
        System.out.println("Beware: This stray Dog will bite");
    }
}

baseclass

Java: Objects as arguments to methods

This entry is part 37 of 54 in the series Learn Java

Java allows us to pass objects as arguments to methods.

Like passing an array, passing an object is actually passing the reference of the object.

This example depicts BookStore application. The showBook() method receives a book object as an argument and displays isbn, author and title of the book.

package javaapplication28;

public class BookClass {

    static class Book {

        int isbn;
        String author;
        String title;

        public void init(int isbn, String author, String title) {
            this.isbn = isbn; // this.isbn refers to instance variable,
            // isbn refers to local variable
            this.author = author;
            this.title = title;
        }
    }
    
    public static class BookStore {

        public void showBook(Book b) {
            System.out.println("ISBN: " + b.isbn + "\nTitle: " + b.title + "\nAuthor: " + b.author);
            System.out.println();
        }
    }

    static public class LibraryBook extends Book {

        int nCopies = 10; // number of copies of the book

        public void show() {
            System.out.println("isbn = " + isbn + "\nauthor = " + author + "\ntitle = " + title + "\ncopies = " + nCopies);
        }
    }
   
    public static void main(String[] args) {

        Book b = new Book();
        b.init(1234, "Peter", "Java");
        BookStore bs = new BookStore();
        bs.showBook(b);

        LibraryBook lb = new LibraryBook();
        lb.init(4321, "Jeniffer", "Data Structures");
        lb.show();

    }
}

inheritance

Java: Inheriting a class with members

This entry is part 36 of 54 in the series Learn Java

Let’s look at an inheritance example with members.

The class LibraryBook is a child class of a super class Book. Here, we say LibraryBook inherits states and behaviors of objects of Book class such as isbn, author, title and many others.

Apart from deriving from Base class, LibraryBook can have its own data members such as number of copies and member functions representing states and behaviors of objects.

package javaapplication31;

public class BookClass {

    static class Book {

        int isbn;
        String author;
        String title;

        public void init(int isbn, String author, String title) {
            this.isbn = isbn; // this.isbn refers to instance variable,
            // isbn refers to local variable
            this.author = author;
            this.title = title;
        }
    }

    static public class LibraryBook extends Book {

        int nCopies = 10; // number of copies of the book

        public void show() {
            System.out.println("isbn = " + isbn + "\nauthor = " + author + "\ntitle = " + title + "\ncopies = " + nCopies);
        }
    }

    public static void main(String[] args) {

        LibraryBook lb = new LibraryBook();
        lb.init(4321, "Jeniffer", "Data Structures");
        lb.show();

    }
}

inheritance

 

Java: Static variables

This entry is part 35 of 54 in the series Learn Java

Static variables have the advantage that they persist throughout the life of the application.

They can therefore, for example, be used to record the number of times that a method has been called across all instances of the class.

The initial value for a static field will only be set once, sometime before the class or field is ever used.

package javaapplication26;

public class StaticMembers {

    static int count = 0;

    static void count() {
        count++;
    }

    void count1() {
        count++;
    }

    public static void main(String[] args) {       

        StaticMembers c = new StaticMembers();
        
        c.count1();
        count();
        c.count1();
        c.count1();
        count();
        
        System.out.println(count);
    }
}

staticvariables

Java: Advantage of static members

This entry is part 34 of 54 in the series Learn Java

The advantage of static members is that they can be used by other classes without having to create an instance of the class. Fields should therefore be declared static when only a single instance of the variable is needed.

Methods should be declared static if they perform a generic function that is independent of any instance variables.

A good example of this is the Math class which contains only static methods and fields.

double pi = Math.PI;

Math is one of the classes that are included by default in every Java application. The reason for this is because it belongs to the java.lang package, which is always imported.

This package contains classes fundamental to the Java language, such as: String,Object and System.

package javaapplication26;

public class StaticMembers {

    public static void main(String[] args) {
        
        double pi = Math.PI;       
        System.out.println(pi);
        
    }
}

staticmembers