Docsity

Abstract Classes-Classes and Data Handling in Java-Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Abstract Classes-Classes and Data Handling in Java-Lecture Slides and more Slides Java Programming in PDF only on Docsity!

Lab Practice: Abstract Classes

 Create an abstract parent class “Livingthing”.

 Add instance variable “name” & use constructor to

initialize it.

 Add following instance methods

  1. Breath()
  2. Eat()
  3. setName()
  4. getName()

 Add following abstract method to it.

 Walk()

Lab Practice: Abstract Classes

 Create 2 child classes “Human”, “Monkey” which inherit from

parent class “Livingthing”.

 Create constructor to initialize instance variable in both child

classes.

 Implement the following “abstract method” of parent class in

both child classes

 Walk()

SOLUTION

public abstract class LivingThing { private String name; public LivingThing(String name){ this.name = name; } public void breath(){ System.out.println("Living Thing breathing..."); } public void eat(){ System.out.println("Living Thing eating..."); } /

  • Abstract method walk() *We want this method to be implemented by subclasses of
  • LivingThing / public abstract void walk();* public String getName() { return name; } public void setName(String name) { this.name = name; } }

SOLUTION

public Human(String name)

super(name);

// Provide implementation of the abstract method.

public void walk()

System.out.println("Human "+getName()+ " walks...");

SOLUTION

public class Main { public static void main(String[] args) { // Create Human object instance and assign it to Human type. Human human1 = new Human(“Ali"); human1.walk(); // Assign Human object instance to LivingThing type. LivingThing livingthing1 = human1; livingthing1.walk(); // Create a Monkey object instance and assign it to LivingThing type. LivingThing livingthing2 = new Monkey(“Sandy"); livingthing2.walk();

SOLUTION

// Display data from human1 and livingthing1. // Observe that they refer to the same object instance. System.out.println("human1.getName() = " + human1.getName()); System.out.println("livingthing1.getName() = " + livingthing1.getName()); // Check of object instance that is referred by x and y is the same object instance. boolean b1 = (human1 == livingthing1); System.out.println("Do human1 and livingthing1 point to the same object instance? "

  • b1); // Compile error // LivingThing z = new LivingThing(); } }

Lab Practice: Interfaces

 Create interface “PersonInterface”

 Add following methods to it.

  1. computeTotalwealth();
  2. getName();

 Create a class “Person” which implements “PersonInterface”

 Add following instance variables to it.

  1. cashSaving
  2. retirementFund
  3. firstName
  4. lastName

 Create constructor to initialize instance variables.

 Implement methods of interface in class “Person”

Lab Practice: Interfaces

 Create another class “Main” which contains main method.  Create an object instance of Person class named as Person1.  Call computeTotalwealth method.  Assign Person object instance to PersonInterface type named as PersonInterface1.  Call computeTotalwealth method.  Display data from person1 and personinterface1.  Check that object instance that is referred by person1 and personinterface type is the same object instance.  Create an object instance of Person class and assign it to the interface type directly named as PersonInterface2.  Call computeTotalwealth method.

SOLUTION

public class Person implements PersonInterface { int cashSaving; int retirementFund; String firstName; String lastName; // Constructor with arguments Person(int cashSaving, int retirementFund, String firstName, String lastName){ this.cashSaving = cashSaving; this.retirementFund = retirementFund; this.firstName = firstName; this.lastName = lastName; } // Compute person's total wealth public int computeTotalWealth(){ System.out.println((cashSaving + retirementFund));; return (cashSaving + retirementFund); } // Get person's name public String getName(){ return firstName + " " + lastName; } }

SOLUTION

public class Main { public static void main(String[] args) { // Create an object instance of Person class. Person person1 = new Person(10000, 20000, "Sang", "Shin"); //You can assign the object instance to // PersonInterface type. PersonInterface personinterface1 = person1; // Display data from person1 and personinterface1. // Observe that they refer to the same object instance. System.out.println("person1.getName() = " + person1.getName() + "," + " person1.computeTotalWealth() = " + person1.computeTotalWealth()); System.out.println("personinterface1.getName() = " + personinterface1.getName()

  • "," + " personinterface1.computeTotalWealth() = " + personinterface1.computeTotalWealth());

OUTPUT

person1.getName() = Sang Shin,

person1.computeTotalWealth() = 30000

personinterface1.getName() = Sang Shin,

personinterface1.computeTotalWealth() = 30000

Do person1 and personinterface1 point to the same object

instance? true

personinterface2.getName() = Dadu Daniel,

personinterface2.computeTotalWealth() = 7000