
Understanding Object-Oriented Programming Principles
"Delve into the fundamentals of OOP principles like constructors, inheritance, polymorphism, and the Liskov Substitution Principle. Explore how to apply these concepts in Java programming with practical examples."
Uploaded on | 0 Views
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Course Introduction CLASS II
Previous studies What is constructor? How to use this()? Where I can call this()? Can I call this() twice in one constructor? What is the meaning between this() and this.XX? 1-2
Previous studies How to use Inheritance? How to use super()? Where can I call super()? What is the meaning between super() and super.XX? What is polymorphism? 1-3
Synopsis of Polymorphism The polymorphism principle Polymorphism means that an operation may behave differently (in different classes). There are two kinds of polymorphism static (overloading) dynamic Example GeomFigure (display(), remove(), position()) Rectangle Circle 1-4
inheritance class class Square IS A kind of rectangle Reuse? Q1: ? Q2: ? 1-5
Class Rectangle class class Rectangle { double double double width; double height; public return } public this.height = height; } public return } public this.width = width; } public double return height; public void double getHeight() { void setHeight(double double height) { public double return width; public void double getWidth() { void setWidth(double double width) { } 1-6
Class Square class class Square extends public super.setHeight(height); super.setWidth(height); } extends Rectangle { public void void setHeight(double double height) { public super.setHeight(width); super.setWidth(width); } public void void setWidth(double double width) { } 1-7
void g(Rectangle r) { r.setWidth(5); r.setHeight(4); if (r.getWidth() * r.getHeight() != 20) { throw new RuntimeException(); } } //some other place Rectangle square = new Square(); g(square); 1-8
Liskov Liskov Substitution Principle (LSP) Substitution Principle (LSP) Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it Very difficult problem!! Further study: OOAD Software design principals Please One of the principals Prefer Composition over Why? Can you give an example? Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it Very difficult problem!! Further study: OOAD Software design principals Please google One of the principals Prefer Composition over inheritnace Why? Can you give an example? google it if you are interested in it it if you are interested in it inheritnace 1-9
Class Class Hierarchy Multiple Inheritance 1-10
Class Is-a Nissan (Is-a) Whole-Part Part-of Has-a (Part-of) (Has-a) 1-11
is ahas a is a has a is a has a A man is a human that has a name, a father, and a mother. public class Man extends Human { Name theName; Father theFather; Mother theMother; } 1-12
Wrong program // Vehicle class Vehicle { Vehicle(String x){ System.out.println( Vehicle s Constructor ); } public void drive(){ System.out.println( I m driving ); } } 1-13
Wrong program // Car extend Vechicle class Car extends Vehicle { } public class app{ public static void main(String[] args){ Car aCar=new Car(); } } Why this program is wrong?? 1-14
How to fix it! // Vehicle class Vehicle { Vehicle(){ System.out.println( Vehicle s Constructor ); } Vehicle(String x){ System.out.println( Vehicle s Constructor ); } public void drive(){ System.out.println( I m driving ); } } 1-15
How to fix it!! // Car extend Vechicle class Car extends Vehicle { Car(){ Super( X ); } } public class app{ public static void main(String[] args){ Car aCar=new Car(); } } } 1-16
How to fix it!! // Car extend Vechicle class Car extends Vehicle { Car(String x){ Super(x); } } public class app{ public static void main(String[] args){ Car aCar=new Car( x ); } } } 1-17
Example class Circle{ double radius; void setRadius(double radius){ this.radius=radius; } void showRadius(){ System.out.println( } double showArea(){ return Math.PI*Math.pow(radius,2); } +radius); } 1-18
API search Search java api document For example pow(a,b) Key in java api pow 1-19
Example public class Application{ public static void main(String argv[]){ Circle aCircle; aCircle = new Circle(); aCircle.setRadius(12.0); aCircle.showRadius(); System.out.println( aCircle + aCircle.showArea()); } } 1-20
How to know new objects hashCode? public class RealWorld { public static void main(String[] args){ Car aCar=new Car(); // Car String x=aCar.toString(); //Name+ @ +hashCode() System.out.println(x); Car bCar=aCar; System.out.println(aCar.toString()); System.out.println(bCar.toString()); } } 1-21
Example detail Circle aCircle; aCircle location l-value:0x1111 Circle Type reference variable aCircle = new Circle(); Circle instance l-value:0x3333 aCircle l-value:0x1111 aCircle = new Circle(); Circle instance l-value:0x3333 aCircle l-value:0x1111 aCircle r-value:0x3333 1-22
Example detail bCircle=aCircle; Circle aCircle bCircle Problem: aCircle=null; ? bCircle=null; Circle aCircle bCircle 1-23
class Vehicle{ int wheel; } Vehicle newCar1 = new Vehicle();// Vehicle newCar2 = new Vehicle();// Vehicle rCar = newCar1; // // // 1-24
(Primitive Type) 8 (Reference Type) So, what is static ? We have not explained it, let us do it now. 1-25