08 Handout 1 PDF - More on Classes
Document Details
Uploaded by FlexibleTonalism3837
STI College
Tags
Related
- Introduction to Java Programming and Data Structures (2019) by Y. Daniel Liang - PDF
- Concepts des langages objet - Programmation Orientée Objet - JAVA PDF
- Cours JAVA : Notions de Base, 2021/2022 PDF
- Java Programming Concepts (Lesson Plan)
- COMP 1000 Computer Science I Exam 2 Review - PDF
- CST 205 Object Oriented Programming using Java PDF
Summary
This document provides an overview of object-oriented programming concepts such as encapsulation, inheritance, polymorphism, and abstraction in Java. It demonstrates these concepts through examples and explanations of Java code. This document is useful for understanding object-oriented programming principles and implementing those in your projects
Full Transcript
SH1801 More on Classes I. Encapsulation The packaging of data and methods into a single component The idea behind encapsulation is to ensure that implementation details are not visible to users. The variables of one class will be hidden from t...
SH1801 More on Classes I. Encapsulation The packaging of data and methods into a single component The idea behind encapsulation is to ensure that implementation details are not visible to users. The variables of one class will be hidden from the other classes, accessible only through the methods of the current class. This is called data hiding. To achieve encapsulation in Java, declare the class' variables as private and provide public setter and getter methods to modify and view the variables' values. For example: public class Student { private String name; public void setName(String name) { this.name = name; } public String getName() { return name; } } II. Inheritance Inheritance is a process that enables one (1) class to acquire the properties (methods and variables) of another. With inheritance, the information is placed in a more manageable, hierarchical order. The class inheriting the properties of another is the subclass (also called derived class, or child class); the class whose properties are inherited is the superclass (base class, or parent class). To inherit from a class, use the extends keyword. For example: class Dog extends Animal { // some code } When one class inherits from another class, it inherits all of the superclass' non-private variables and methods. For example: class Animal { protected int legs; public void eat() { System.out.println("Animal eats"); } } class Dog extends Animal { Dog() { legs = 4; } } 08 Handout 1 *Property of STI [email protected] Page 1 of 3 SH1801 class MyClass { public static void main(String[ ] args) { Dog d = new Dog(); d.eat(); } } Constructors are not member methods, and so are not inherited by subclasses. However, the constructor of the superclass is called when the subclass is instantiated. For example: class A { public A() { System.out.println("New A"); } } class B extends A { public B() { System.out.println("New B"); } } class Program { public static void main(String[ ] args) { B obj = new B(); } } III. Polymorphism Polymorphism, which refers to the idea of "having many forms", occurs when there is a hierarchy of classes related to each other through inheritance. A call to a member method will cause a different implementation to be executed, depending on the type of the object invoking the method. IV. Abstract Classes A. Abstraction Data abstraction provides the outside world with only essential information, in a process of representing essential features without including implementation details. The concept of abstraction is that we focus on essential qualities, rather than the specific characteristics of one (1) particular example. In Java, abstraction is achieved using abstract classes and interfaces. An abstract class is defined using the abstract keyword. o If a class is declared abstract, it cannot be instantiated (you cannot create objects of that type). o To use an abstract class, you have to inherit it from another class. o Any class that contains an abstract method should be defined as abstract. 08 Handout 1 *Property of STI [email protected] Page 2 of 3 SH1801 B. Abstract Class For example, we can define our Animal class as abstract. abstract class Animal { int legs = 0; abstract void makeSound(); } The makeSound() method is also abstract, as it has no implementation in the superclass. We can inherit from the Animal class and define the makeSound() method for the subclass: class Cat extends Animal { public void makeSound() { System.out.println("Meow"); } } V. Interfaces An interface is an abstract class that contains only abstract methods. Some specifications for interfaces are: o Defined using the interface keyword o May contain only static final variables o Cannot contain a constructor because interfaces cannot be instantiated o Interfaces can extend other interfaces o A class can implement any number of interfaces An example of a simple interface: interface Animal { public void eat(); public void makeSound(); } Interfaces have the following properties: o An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface. o Each method in an interface is also implicitly abstract, so the abstract keyword is not needed. o Methods in an interface are implicitly public. Use the implements keyword to use an interface with your class. interface Animal { public void eat(); public void makeSound(); } class Cat implements Animal { public void makeSound() { System.out.println("Meow"); } public void eat() { System.out.println("omnomnom"); } } References: Deitel, H., & Deitel, P. (2014). Java: How to program-early objects (10th ed.). Prentice Hall. SoloLearn.com – Java Programming. Retrieved on March 07, 2018 from https://www.sololearn.com/Play/Java# 08 Handout 1 *Property of STI [email protected] Page 3 of 3