Download presentation
Presentation is loading. Please wait.
1
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2
2
CSM-Java Programming-I Lesson-1 final Keyword Any number that is not completely self- explanatory should be declared as a named constant. A final variable is a constant. Once its value has been set, it cannot be changed. Use named constants to make your programs easier to read and maintain. Eg: final int DAYS_PER_YEAR = 365;
3
CSM-Java Programming-I Lesson-1 static Keyword Java numbers are not objects, so you cannot invoke a method on a number. Eg: To calculate sqrt or power. A static method does not operate on an object. Eg: Math.sqrt(x); Math.round(y);
4
CSM-Java Programming-I Lesson-1 Static Revisited When a member is declared static, it can be accessed before any objects of its class are created. Both methods and variables can be declared static. A static member is a member that is only one per class, rather than one in every object created from that class. A static method can access only static variables and static methods of the class. There is no this reference because there is no specific object being operated on. classname.staticmethod();
5
CSM-Java Programming-I Lesson-1 Inheritance When a class B acquires the properties of another class A, then class B is said to have inherited class A. Here, class A is a superclass and class B is a subclass. A subclass inherits all the instance variables and methods defined by the superclass and adds its own, unique elsements. The keyword extends is used to inherit a class. Eg: class subclass extends superclass {
6
CSM-Java Programming-I Lesson-1 Inheritance Every class in Java is an extended class, whether it is declared with an extends keyword or not. If a class does not explicitly extend from another class, it implicitly extends the Object class.
7
CSM-Java Programming-I Lesson-1 Example: Inheritance public class BankAccount { int acctNum; String name; public BankAccount(int bAcctNum, String name) { acctNum = bAcctNum; ……. } public double getBalance () { …… }
8
CSM-Java Programming-I Lesson-1 Example: Inheritance public class CheckingAccount extends BankAccount { double interest; public CheckingAccount(int aNum, String aName, double aInt) { super (aNum, aName); interest = aInt; } public double calculateInterest() { ……. }}
9
CSM-Java Programming-I Lesson-1 super Whenever a subclass class super(), it is calling the constructor of its immediate superclass. super() must be the first statement executed inside a subclass constructor. super.member always uses the superclass’s member (member could be a method or an instance variable)
10
CSM-Java Programming-I Lesson-1 Example : super public class BankAccount { protected double getBalance() { return 1000.00; } public class SavingsAccount extends BankAccount{ protected double getBalance() { return 1010.00; }
11
CSM-Java Programming-I Lesson-1 super protected void printBalance() { BankAccount ba = this; //superclass variable //can reference //subclass object. System.out.println(super.getBalance()); System.out.println(this.getBalance()); System.out.println(ba.getBalance()); } Output: 1000.00 1010.00
12
CSM-Java Programming-I Lesson-1 Method Overriding When a method in a subclass has the same name and type signature and a method in its superclass, then the method in the subclass is said to override the method in the subclass. Only non-static methods can be overridden. Both signature and return type must be the same as the superclass. The throws clause of an overriding method can have fewer types listed than the method in the superclass, or more specific types or both.
13
CSM-Java Programming-I Lesson-1 Example: Method Overriding public class Parent { public void hello(){ System.out.println(“Hello from parent”); } public class Child extends Parent { public void hello(){ System.out.println(“Hello from Child”); }
14
CSM-Java Programming-I Lesson-1 Method Overriding An extended class can change the access of a superclass’s methods, but only if it provides more access. A method declared protected in the super class can be redeclared protected or public, but not private. Fields cannot be overridden; they can only be hidden. To access the hidden fields use the super keyword.
15
CSM-Java Programming-I Lesson-1 final and Inheritance A method declared final cannot be overridden A class can also be declared final. Such a class cannot be extended. final class Security { // } A final class’s methods are implicitly final. static and private methods cannot be overridden.
16
CSM-Java Programming-I Lesson-1 Abstract Classes A superclass that only defines a generalized form that will be shared by all its subclasses, leaving the implementation details to its subclasses is said to an abstract class. A concrete class has concrete methods, including implementations of any abstract methods inherited from its superclasses. Any class with abstract methods should be declared abstract.
17
CSM-Java Programming-I Lesson-1 Example: Abstract Class abstract class Shape { abstract double area(); public void display () {// concrete method // Do something } class Square extends Shape { double area() { // Do something }
18
CSM-Java Programming-I Lesson-1 Abstract Classes An abstract class cannot have objects because it is not complete, but it can have references. Static methods and constructors cannot be declared abstract. Any subclass of an abstract class must either implement all the abstract methods in the superclass or be itself declared abstract. A concrete method can be overriden to become abstract. It is illegal to declare a class both final and abstract.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.