Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.

Similar presentations


Presentation on theme: "CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17."— Presentation transcript:

1 CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17

2 Inheritance – Private Variables private class Animal { private String name; public String getName() { return name; } public void setName(String s() { name = s; } private class Zebra extends Animal{ private int numStripes; public Zebra(String s, int p) { numStripes = p; name = s; } //Other methods } Name is private in super class. Can’t directly access in subclasses. How can you access it then?

3 Inheritance – Private Variables private class Animal { private String name; public String getName() { return name; } public void setName(String s() { name = s; } private class Zebra extends Animal{ private int numStripes; public Zebra(String s, int p) { numStripes = p; setName(s); } //Other methods } Call the public mutator (setter) method.

4 Public Methods public class AnimalTester { public static void main (String [] args) { Zebra z = new Zebra (“Rex”, 512); z.setName(“Jax”); } Even though setName is declared in the Animal class, you can still access it from a sub-class of animal (because its public). Remember inheritance is the is-a relationship. A Zebra is-an Animal.

5 Implications of Inheritance on Constructors private class Animal { private String name; public Animal(String temp) { name = temp; } //Other stuff } private class Animal { private String name; public Animal(String temp) { name = temp; } //Other stuff } private class Zebra extends Animal{ private int numStripes; public Zebra (int val){ super(“Stan”); numStripes = val; } private class Zebra extends Animal{ private int numStripes; public Zebra (int val){ super(“Stan”); numStripes = val; } Notice there is no constructor in Animal that accepts zero args. So any subclass MUST send info to the one constructor that is part of Animal. super(…) allows the programmer to send info to a super class constructor must be the first line of code in the sub class constructor… Remember: Base class constructors are called first. They are executed from the top of the hierarchy down. Remember: Base class constructors are called first. They are executed from the top of the hierarchy down.

6 Implications of Inheritance on Constructors private class Animal { private String name; public Animal(String temp) { name = temp; } //Other stuff } private class Animal { private String name; public Animal(String temp) { name = temp; } //Other stuff } private class Zebra extends Animal{ private int numStripes; public Zebra (int val, String n){ super(n); numStripes = val; } private class Zebra extends Animal{ private int numStripes; public Zebra (int val, String n){ super(n); numStripes = val; } Notice there is no constructor in Animal that accepts zero args. So any subclass MUST send info to the one constructor that is part of Animal. We can pass a value sent to the zebra constructor up to the Animal constructor. Remember: Base class constructors are called first. They are executed from the top of the hierarchy down. Remember: Base class constructors are called first. They are executed from the top of the hierarchy down.

7 Accessing Super-class version of method private class Animal { private String name; public Animal(String temp) { name = temp; } public void print() { System.out.println(“My name is “ + name + “.”); } private class Animal { private String name; public Animal(String temp) { name = temp; } public void print() { System.out.println(“My name is “ + name + “.”); } private class Zebra extends Animal{ private int numStripes; public Zebra (int val, String s){ super(s); numStripes = val; } public void print() { super.print(); System.out.println(“I am a zebra with “ + numStripes + “ stripes.”); } private class Zebra extends Animal{ private int numStripes; public Zebra (int val, String s){ super(s); numStripes = val; } public void print() { super.print(); System.out.println(“I am a zebra with “ + numStripes + “ stripes.”); } print is an overridden.

8 Protected Access public class Animal { protected String name; //setters and getters } Protected Access – can be accessed directly by the class in which it is declared and any subclass. public class Zebra extends Animal { private int numStripes; public Zebra(String s, int num) { name = s; numStripes = num; } Since Zebra extends Animal, the programmer can directly access name from methods in class Zebra. However, name is still inaccessible outside Animal or any class that extends Animal (so it couldn’t be accessed from main()).


Download ppt "CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17."

Similar presentations


Ads by Google